In my JSP Servlet application, One of the JSP page is able to send in form parameters to Servlet while other throws Null Pointer exception while doing the same thing. Why so? Will deeply appreciate help on this, please find code below.
Name.jsp
file
<%@ 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
file
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
<%@ 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
file
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");
}
}
I am able to go from Name.jsp
page to Bio.jsp
page and access attributes in NameServlet
but as soon as I enter values in Bio.jsp
page and try to redirect it to next page in application, I run into a null pointer exception. stacktrace below.
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)
The request.getAttribute("age")
is coming as null
from Bio.jsp
page, I will deeply appreciate any help on this.