我正在开发一个J2EE项目,我想拥有这个架构:
JSP1:index.jsp
Servlet:搜索
JSP2:results.jsp
我想在jsp1中插入一些数据,在servlet处理中使用它然后在jsp2中显示结果。
因此,我不能在jsp2中得到结果。我还尝试将一个测试变量放入servlet并发送到jsp2,但它也没有用。 我怎样才能解决这个问题? 这是我的jsp1:
`<%@ page langua`ge="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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">
</head>
<body>
<form action="search " method="post">
var1: <input type="text" name="var1" size="20">
var2: <input type="text" name="var2" size="20">
<input type="submit" value="Search" />
</form>
</body>
</h
这是我的servlet类
package servlet;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import forms.WriteExcel;
public class Search extends HttpServlet {
private static final long serialVersionUID = 1L;
public Search() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/index.jsp");
rd.forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String var1 = request.getParameter("var1");
String var2 = request.getParameter("var2");
String[] info = { var1, var2 };
HashMap<String, ArrayList<String>> files;
String resultsExcelPath = "";
try {
files = Main(info);
resultsExcelPath = WriteExcel.csvFile(info, files);
request.setAttribute("files", files);
request.setAttribute("excel", excel);
request.setAttribute("variable test", "just a variable to test jsp2");
this.getServletContext().getRequestDispatcher("/WEB-INF/results.jsp").forward(request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
}
这是我的jsp2:results.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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">
</head>
<body>
<p>variable : ${variable}</p>
<p>var2 : ${var2}</p>
</body>
</html>
这是我的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>test</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Search</servlet-name>
<servlet-class>servlet.Search</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Search</servlet-name>
<url-pattern>/search</url-pattern>
</servlet-mapping>
</web-app>