我正在使用Servlet,JSP模型在JSP复选框中显示多个值。复选框是动态创建的。 (我有一个字符串,将其删除以获取用作复选框值的注释值。)复选框值显示在屏幕上。但是,我看不到捕获的选择(例如,有5个选项可供选择,我检查2个框。)
我也尝试过jsp文件。给我空指针错误。
<%
System.out.println("Values selected: ");
String[] checkedIds = request.getParameterValues("checkedRows");
System.out.println(" " + checkedIds[0] + " " + checkedIds[1]);
%>
QueryString1.java
package g6gQRY.QRYstring;
import g6gQRY.annotateQRY.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.*;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.ServletRequest;
public class QueryString1 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String querystring =request.getParameter("querystring");
System.out.println("Query String is: " + querystring);
ArrayList<String> A1 = new ArrayList<String>();
annotateQuery.queryToAnnotate(querystring, A1);
request.setAttribute("questring", A1);
String[] checkedIds = request.getParameterValues("checkedRows");
RequestDispatcher view = request.getRequestDispatcher("queryResult.jsp");
view.forward(request, response);
// println not working, shows null
// String arrayAsString = Arrays.toString(checkedIds);
// System.out.println("Values selected: " + arrayAsString);
}}
}
queryResult.jsp是:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.util.*" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1> Query Results JSP </h1>
<br><br>
<p>
<table>
<c:forEach var="annotateVar" items="${questring}">
<tr><td> <input type="checkbox" name="checkedRows" value="${annotateVar}">
<c:out value="${annotateVar}" /> </td></tr>
</c:forEach>
<tr><td align="center"><input type="submit" value="Submit"></td></tr>
</table>
</body>
</html>
web.xml文件是:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<description></description>
<servlet-name>QueryString1</servlet-name>
<servlet-class>g6gQRY.QRYstring.QueryString1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>QueryString1</servlet-name>
<url-pattern>/queryStr</url-pattern>
</servlet-mapping>
</web-app>