Name.jsp
文件
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>What's Your Name</h1>
<form action="Name" method="POST">
First Name: <input type="text" name="firstname"></br>
Last Name: <input type="text" name="lastname"></br>
<input type="submit" value="Submit">
</form>
</body>
</html>
NameServlet
文件
package com.servelets; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.objects.Name; /** * Servlet implementation class NameServlet */ @WebServlet("/Name") public class NameServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public NameServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("Get Name"); response.getWriter().append("Served at: ").append(request.getContextPath()); request.getRequestDispatcher("WEB-INF/views/Name.jsp").forward(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("Post Name"); Name name = new Name(); HttpSession session = request.getSession(); name.setFirstname(request.getParameter("firstname")); name.setLastname(request.getParameter("lastname")); session.setAttribute("name", name); //request.getRequestDispatcher("/WEB-INF/views/Bio.jsp").forward(request, response); response.sendRedirect("Bio"); //request.getRequestDispatcher("Bio").include(request, response); } }
[Bio.jsp
页面
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> </head> <body> <h1>Give us your Age, Weight, Height</h1> <h1>Welcome ${name.firstname} ${name.lastname} </h1> <form action="Bio" method="POST"> Age: <input type="text" name="age"></br> Weight: <input type="number" name="weight"> <select id="weightunit" name="weightunit"> <option value="kg">Kg</option> <option value="Lb">Lb</option> </select></br> Height: <input type="number" name="height"> Unit: <input type="text" name="heightunit"></br> <input type="submit" value="Submit"> </form> </body> </html>
BioServlet
文件
package com.servelets; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.objects.Bio; /** * Servlet implementation class BioServlet */ @WebServlet(description = "Servlet for Bio Page", urlPatterns = { "/Bio" }) public class BioServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public BioServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub System.out.println("Get Bio"); response.getWriter().append("Served at: ").append(request.getContextPath()); request.getRequestDispatcher("WEB-INF/views/Bio.jsp").forward(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub System.out.println("Post Bio"); Bio bio = new Bio(); if(request.getAttribute("age")==null) { System.out.println("age is null"); } HttpSession session = request.getSession(); String age = (String) request.getAttribute("age"); bio.setAge(Integer.parseInt(age)); bio.setHeight(Double.parseDouble(request.getAttribute("height").toString())); bio.setHeightunit(request.getAttribute("heightunit").toString()); bio.setWeight(Double.parseDouble(request.getAttribute("weight").toString())); bio.setWeightunit(request.getAttribute("weightunit").toString()); session.setAttribute("bio", bio); response.sendRedirect("Address"); } }
我能够从Name.jsp
页面转到Bio.jsp
页面并访问NameServlet
中的属性,但是一旦我在Bio.jsp
页面中输入值并尝试将其重定向到应用程序的下一页,我就会遇到空指针异常。下面的堆栈跟踪。
May 16, 2020 5:35:53 PM org.apache.catalina.startup.Catalina start INFO: Server startup in [3,151] milliseconds Get Name Post Name Get Bio Post Bio age is null May 16, 2020 5:38:40 PM org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service() for servlet [com.servelets.BioServlet] in context with path [/DisplayAddress] threw exception java.lang.NumberFormatException: null at java.base/java.lang.Integer.parseInt(Integer.java:614) at java.base/java.lang.Integer.parseInt(Integer.java:770) at com.servelets.BioServlet.doPost(BioServlet.java:50) at javax.servlet.http.HttpServlet.service(HttpServlet.java:660) at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
request.getAttribute("age")
从null
页面以Bio.jsp
的形式出现,对此深表感谢。
BioServlet
文件中的代码,它正常工作,更新的代码如下所示。 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("Post Bio");
Bio bio = new Bio();
if(request.getParameter("age")==null) {
System.out.println("age is null");
}
HttpSession session = request.getSession();
String age = (String) request.getParameter("age");
bio.setAge(Integer.parseInt(age));
bio.setHeight(Double.parseDouble(request.getParameter("height").toString()));
bio.setHeightunit(request.getParameter("heightunit").toString());
bio.setWeight(Double.parseDouble(request.getParameter("weight").toString()));
bio.setWeightunit(request.getParameter("weightunit").toString());
session.setAttribute("bio", bio);
response.sendRedirect("Address");
}