To store value in session & display it

To store value in session & display it

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();
        }
    }
View Answers

Ads









Related Tutorials/Questions & Answers:
To store value in session & display it
To store value in session & display it  Hello..I m trying to run the following code which will store the session of a book selected on click...="add"> <!-- <input type="checkbox" name="book1cat" value
Display session value Using Servlet
Display session value Using Servlet     ... will be invalidated automatically. A negative value indicates that the session...: In case of first time accessing of servlet the following session value
Advertisements
ModuleNotFoundError: No module named 'display-session'
ModuleNotFoundError: No module named 'display-session'  Hi, My... named 'display-session' How to remove the ModuleNotFoundError: No module named 'display-session' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'display-session'
ModuleNotFoundError: No module named 'display-session'  Hi, My... named 'display-session' How to remove the ModuleNotFoundError: No module named 'display-session' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'display-session'
ModuleNotFoundError: No module named 'display-session'  Hi, My... named 'display-session' How to remove the ModuleNotFoundError: No module named 'display-session' error? Thanks   Hi, In your python
Display of value in Tabel cell
Display of value in Tabel cell  I have added a custom field..., Object value , boolean isSelected, int row, int column){ JPanel...(editor.getTableCellEditorComponent(table, value, isSelected, row, column)); panel.add
Version of com.aerospike>aerospike-session-store dependency
List of Version of com.aerospike>aerospike-session-store dependency
Version of com.nimbusds>oidc-session-store dependency
List of Version of com.nimbusds>oidc-session-store dependency
ModuleNotFoundError: No module named 'odoo12-addon-muk-session-store'
ModuleNotFoundError: No module named 'odoo12-addon-muk-session-store' ...: No module named 'odoo12-addon-muk-session-store' How to remove the ModuleNotFoundError: No module named 'odoo12-addon-muk-session-store' error
ModuleNotFoundError: No module named 'odoo12-addon-muk-session-store'
ModuleNotFoundError: No module named 'odoo12-addon-muk-session-store' ...: No module named 'odoo12-addon-muk-session-store' How to remove the ModuleNotFoundError: No module named 'odoo12-addon-muk-session-store' error
ModuleNotFoundError: No module named 'scylla_django_session_store'
ModuleNotFoundError: No module named 'scylla_django_session_store'  ...: No module named 'scylla_django_session_store' How to remove the ModuleNotFoundError: No module named 'scylla_django_session_store' error? Thanks
. Display JavaScript dropdown selected value
you select any value from list, alert will display that. In var.... Display JavaScript dropdown selected value  How to get the selected value from dropdown box? I am using javascript.   <html> <
how to store jtable value in multidimensional array?
how to store jtable value in multidimensional array?   i want to store the value of jtable in multidimensional array,with type double. how to store jtable value in multidimensional array
how to store jtable value in multidimensional array?
how to store jtable value in multidimensional array?   i want to store the value of jtable in multidimensional array,with type double. how to store jtable value in multidimensional array
store dynamic generated textbox value into database
store dynamic generated textbox value into database  sir, how do i store dynamically generated textbox value into the database thanks in advance
store value in checkbox from gridview
store value in checkbox from gridview  Please check the error...; <input type="submit" value=" DELETE "/>&nbsp;&nbsp..." name="chbox<%=no%>" value="<%= rst.getString("name") %>" ></td>
session value not get in many jsp page.
session value not get in many jsp page.  I am using servlet to set session by following code :- HttpSession session=request.getSession(false...,response); and get session value on jsp page by follwing:- String user=(String
Maven Dependency aerospike-session-store >> 0.9
You should include the dependency code given in this page to add Maven Dependency of com.aerospike >> aerospike-session-store version0.9 in your project
Maven Dependency aerospike-session-store >> 0.9.1
You should include the dependency code given in this page to add Maven Dependency of com.aerospike >> aerospike-session-store version0.9.1 in your project
Maven Dependency aerospike-session-store >> 1.1
You should include the dependency code given in this page to add Maven Dependency of com.aerospike >> aerospike-session-store version1.1 in your project
Maven Dependency aerospike-session-store >> 1.2
You should include the dependency code given in this page to add Maven Dependency of com.aerospike >> aerospike-session-store version1.2 in your project
Maven Dependency oidc-session-store >> 5.3
You should include the dependency code given in this page to add Maven Dependency of com.nimbusds >> oidc-session-store version5.3 in your project
How many cookie object we can store in a session? is there any limit?
How many cookie object we can store in a session? is there any limit?  How many cookie object we can store in a session? is there any limit
how to display the value of termcell name in label
how to display the value of termcell name in label  want to display the tercell name in label report of pending apllication of termcellname code...="display"%> <%@ taglib uri="/WEB-INF/tlds/struts-html.tld" prefix="html"%> <
store html value in the sqllite 3 php
store html value in the sqllite 3 php  i have a php script that will parse html page content link and view but i need to know how to store... tell db to store html content from $articles $result = $db->query('SELECT bar
java script display value - Java Beginners
java script display value  my code is below: my problem is: the value 'firstname' is not showed with getparameter in .jsp file? (when click submit button in form) Main file: Name: file java
how to display textbox value based on selected option value?
how to display textbox value based on selected option value?  Hi,I... on first value I finished above code. ID: xx Name:xx xx is baesd on second and first value. But it is that l can't do that. now, l post my code
I want to store the value of local variable into Global variable
I want to store the value of local variable into Global variable    <%=cnt%>=x; is it a valid Statement
How to store two integer value in a separate file and retrieve those values
How to store two integer value in a separate file and retrieve those values  I need to save two values so i can use those values later i came up... can i write and update value in that file
session
session   explain sessioln tracking in jsp and display program
how to use dropdown list in JSP and display value in same page
how to use dropdown list in JSP and display value in same page  I... one option the value must get displayed below it in the same page as: Value selected = B Now if choose another option, new message should be printed

Ads