Using data from JSP file in servlet

Here’s the JSP:

<%@ page language="java" contentType="text/html; charset=windows-1256"
<%@page import="com.exalt.servlets.LoginServlet"%>
<%@page import="com.exalt.classes.MCategory"%>
<%@page import="com.exalt.classes.SCategory"%>
<!DOCTYPE htmlPUBLIC"//W3C//DTDHTML4.01Transitional//EN"" dtd">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256">
<title>Add sub category</title>
<h2>add a category</h2>
<form  method="post" action="AddSubCategServlet">
name  <input type="text" name="subcategory" style="margin:10px"><br><br>
Main category    
<select size="1" id="MCategory" title="" name="MCategory">
<option value="">Select a main category</option>  
    for( MCategory elem : LoginServlet.MCList) {
            <option value=<%=elem.getName()%>><%= elem.getName()%></option> 
            <%} %>     
<input type="submit" value="Add">

and here’s the servlet :

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    String sub= request.getParameter("subcategory");
    String main= request.getParameter("MCategory");

    SCategory SCat=new SCategory(sub);
        MCategory selected = LoginServlet.MCList.get(0);

        request.getRequestDispatcher("CategDetails.jsp").forward(request, response);


I want to use the index of the selected element from the dropdown list in the servlet. there’s a method .index I thinks but I can’t use it here

Source: jsp

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.