在servlet中使用JSP文件中的数据

时间:2015-07-06 07:58:09

标签: java html jsp servlets

这是JSP:

<%@ page language="java" contentType="text/html; charset=windows-1256"
pageEncoding="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""http://www.w3.org/TR/html4/loose. dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256">
<title>Add sub category</title>
</head>
<body>
<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> 
            <%} %>     
</select><br><br>
<input type="submit" value="Add">
</form>
 </body>
 </html>

这里是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);
        }

}

我想使用servlet下拉列表中所选元素的索引。有一种方法。我认为是指数,但我不能在这里使用

1 个答案:

答案 0 :(得分:0)

使用http://www.w3schools.com/jsref/prop_select_selectedindex.asp

中的selectObject.selectedIndex

您可以在选择自己或提交前使用selectedIndex设置选择框值