【问题标题】:Variables passing from servlet to JSP not working从 servlet 传递到 JSP 的变量不起作用
【发布时间】:2023-03-09 15:25:01
【问题描述】:

我正在从事一个 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>

【问题讨论】:

    标签: jsp servlets post jakarta-ee get


    【解决方案1】:

    使用地图params:例如:${params.var1}

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    </head>
    <body>
        <p>variable : ${params.var1}</p>
        <p>var2 : ${params.var2}</p>
    </body>
    </html>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-10
      • 2011-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-22
      • 1970-01-01
      • 2011-11-13
      相关资源
      最近更新 更多