
Hello..I m trying to run the following code which will store the session of a book selected on click of the button.But first it was showing NullPointerException and then blank page..Please Help me in this.. This is the JSP Page
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Book Catalog1</title>
</head>
<body>
<form action="/Books/addtocart" target="add">
<!-- <input type="checkbox" name="book1cat" value="JSP" />JSP
<input type="checkbox" name="book1cat" value="C" />C
<input type="checkbox" name="book1cat" value="Java" />Java
<input type="checkbox" name="book1cat" value="Servlet" />Servlet<br/>-->
<b>Jsp</b><input type="submit" value="Add JSP To Cart" name="ADD" /><br/>
<b>Java</b><input type="submit" value="Add Java To Cart" name="ADDJSp" /><br/>
</form>
</body>
</html>
This is the servlet page where i m storing the value(this code is for selecting the first book only):
import java.io.IOException;
import java.io.PrintWriter;
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;
public class addtocart extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
HttpSession hs=request.getSession(true);
if(hs.isNew()){
hs.setAttribute("ADD", new int[] {0});
}
int[] ADD=(int[])hs.getAttribute("ADD");
try {
if((String)request.getParameter("ADD")!=null){
ADD[0]++;
out.print("Brought a JSP book.You now have "+ADD[0]+ "JSP book");
}
out.print("<HTML><HEAD><TITLE>Shopping Cart</TITLE></HEAD><BODY>");
out.print("<HR><A HREF='/Books/Book1.jsp'>Go to Main</a>");
} finally {
out.close();
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.