我在 STS 中创建了一个 Maven 项目。在项目中,我有 1 个 jsp 文件和 1 个 servlet。jsp 要求用户输入用户名和密码,然后 servlet 获取值,以 HTML 格式显示。但我也有 1 个 java 类。我希望将 jsp 文件中输入的数据存储在 java 类中定义的 2 个变量中,但我无法这样做。我尝试了几件事,但无法理解如何去做。
你好Servlet:
public class HelloServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public HelloServlet() {
super();
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("<title>My Intro Servlet!</title>");
out.println("<h1>Welcome " + request.getParameter("name") + " "
+ "Password: " + request.getParameter("Password") + "!</h1>");
out.println("<p><a href = http://localhost:9080/HelloWorldWeb/Hello.jsp>"
+ "<input type = submit value = Back /></a></p>");
String User = request.getParameter("Name");
String Password = request.getParameter("Password");
User user1 = new User();
user1.setUser(request.getParameter("name"));
user1.setPassword(request.getParameter("Password"));
System.out.println(User);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
用户 Java 类:
public class User {
private String user;
private String password;
public User() {
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
仅 JSP 表单:
form action="hello.html" method="GET"
doGet Name: <input type="text" name="name">
doGet Password: <input type="Password" name="Password" />
<input type="submit" value="Submit" />
<input type="reset" value="Reset">
XML 文件:
<display-name>HelloWorldWeb</display-name>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>Hello.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>HelloServlet</display-name>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>com.javavids.webapp.HelloWorldWeb.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/hello.html</url-pattern>
</servlet-mapping>
</web-app>