gpt4 book ai didi

java - 如何通过 ServletContext 的属性机制使 servlet 接收到的属性可供其他 servlet 使用?

转载 作者:行者123 更新时间:2023-12-01 11:48:04 24 4
gpt4 key购买 nike

我必须使用表单从 servlet 获取信息,然后通过 ServletContext 的属性方法将信息(字符串)提供给其他 servlet?我一点也不理解这一点,并且非常感谢逐步的分割。这是我的代码。一个 Servlet 采用整数和 float ,另一个采用姓名。

第一个 Servlet

package myServlets;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;


public class FirstServlet extends GenericServlet {

public String firstName;
public String surname;

@Override
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException{


PrintWriter out = res.getWriter();
out.println("<HTML><HEAD><TITLE>");
out.println("ServletResponse");
out.println("</TITLE></HEAD>");
out.println("<BODY>");
out.println("<H1>First name: " + req.getServletContext().getAttribute("firstName") + "</H1><br>");
out.println("<H1>Surname: " + req.getServletContext().getAttribute("surname") + "</H1><br>");
out.println("<H1>Integer: " + req.getServletContext().getAttribute("integer") + "</H1><br>");
out.println("<H1>Floating Point: " + req.getServletContext().getAttribute("float") + "</H1><br>");
out.println("<br><br><br><a href=\"index.html\">Back to the forms! </a>");
out.println("</BODY>");
out.println("</HTML>");
}
}

我的第二个 Servlet

package myServlets;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;

public class SecondServlet extends GenericServlet {
public String noInteger;
public String noFloat;


@Override
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException{


PrintWriter out = res.getWriter();
out.println("<HTML><HEAD><TITLE>");
out.println("ServletResponse");
out.println("</TITLE></HEAD>");
out.println("<BODY>");
out.println("<H1>First name: " + req.getServletContext().getAttribute("firstName") + "</H1><br>");
out.println("<H1>Surname: " + req.getServletContext().getAttribute("surname") + "</H1><br>");
out.println("<H1>Integer: " + req.getServletContext().getAttribute("integer") + "</H1><br>");
out.println("<H1>Floating Point: " + req.getServletContext().getAttribute("float") + "</H1><br>");
out.println("<br><br><br><a href=\"index.html\">Back to the forms! </a>");
out.println("</BODY>");
out.println("</HTML>");
}
}

我的 Index.html 文件

 <html>
<head>
<title>Lab One</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
First Name: <input type="text" name="firstName">
<br>
Surname: <input type="text" name="surname">
<br>
<form action="FirstServlet" method="POST">
<input type ="submit" name="Submit"> </form>
<br>
Integer: <input type="text" name="integer">
<br>
Float: <input type="text" name="float">
<br>

<form action="SecondServlet" method="POST">
<input type ="submit" name="Submit2"> </form>

</body>
</html>

最佳答案

为什么不将其添加为 session 属性

HttpSession session = request.getSession();
String firstName= (String)request.getAttribute("firstName");
session.setAttribute("firstName", firstName);

然后其他 servlet/jsp 可以读取该值

session.getAttribute ("firstName");

为了简单起见,您还可以将整个用户类(??)设置为 session 属性

编辑,因为您使用的是 ServletRequest 而不是 HttpServletRequest,请尝试使用以下内容来获取 servletContext

http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getServletContext()

关于java - 如何通过 ServletContext 的属性机制使 servlet 接收到的属性可供其他 servlet 使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28999974/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com