Request.getattribute在一个Servlet中工作,而在其他[重复]中抛出空指针

问题描述 投票:0回答:1
在我的JSP Servlet应用程序中,JSP页面之一能够将表单参数发送给Servlet,而其他页面在执行相同操作时会抛出Null Pointer异常。为什么这样?非常感谢您提供帮助,请在下面找到代码。

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的形式出现,对此深表感谢。 
java jsp tomcat servlets
1个回答
0
投票
[我意识到我使用的是Request.getAttribute而不是Request.getParameter,我更新了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"); }

© www.soinside.com 2019 - 2024. All rights reserved.