deeban
deeban

Reputation: 149

How to send Form field values as session from jsp to servlet

I have created the project for hospital management.. I want to send the form field values from jsp to servlet as session... I have tried the following code

Jsp page:

 <html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Add patient details</title>
</head>
<body>


    <form action="Patientdetails" method="GET">


        <fieldset> 
            <legend> <h1> Patient details </h1> </legend>


            <center>
                <table> <tr><td>


                            Patient's name: </td><td> <input type="text" name="name" placeholder="Enter the patients's name" value="deeban" pattern='[A-Za-z0-9]{5,15}' title="Doctor name should be minimum of 5 characters and maximum of 15 characters"/> </td></tr><tr><td><br></td></tr><tr><td>

                            Patient's age: </td><td> <input type="text" name = "age" placeholder="Enter the patients's age" pattern='[0-9]{2}' title="Enter age as two digit number"/> </td></tr><tr><td><br></td></tr><tr><td>                   

                            Gender : </td><td> <label for='male'>male</label> <input type="radio" name = "gender" id='gender' value='male' checked /> 
                            <label for ='female'>female</label><input type='radio' name = "gender" id='gender' value='female' /> </td></tr><tr><td><br></td></tr><tr><td>  
                            Category of doctor to meet: </td><td><select name="cat">
                                <optgroup label="Select Category">
                                    <option value="Dentist"> Dentist</option>
                                    <option value="Cardiologist"> Cardiologist </option>
                                    <option value="surgeon" selected>surgeon </option>
                                    <option value="Diabetologists"> Diabetologists‎</option>
                                </optgroup> </select> </td></tr><tr><td><br></td></tr><tr><td>   
                            Visiting time: </td><td><select name= "time" multiple>
                                <optgroup label="Available timings">
                                    <option value="9:00A.M-12:00P.M (Morning)"> 9:00A.M-12:00P.M (Morning)</option>
                                    <option value="12:00P.M-3:00P.M (Afternoon)">12:00P.M-3:00P.M (Afternoon)</option>
                                    <option value="04:00P.M-7:00P.M (Evening)" selected>04:00P.M-7:00P.M (Evening)</option>
                                    <option value="8:00P.M-12:00A.M (Night)">8:00P.M-12:00A.M (Night)</option>
                                </optgroup> </select> </td></tr><tr><td> <br></td></tr><tr><td>

                            Your token number:</td><td> <input type="text" name="token" value="14" readonly/>                            
                        </td></tr><tr><td><br></td></tr><tr><td> 

                            <input type='submit' value='submit'> </td></tr>

                    <%

                        try {
                            String name, age, gender, cat, time, token;
                            name = (String) request.getAttribute("name");
                            age = request.getParameter("age");
                            gender = request.getParameter("gender");
                            cat = request.getParameter("cat");
                            time = request.getParameter("time");
                            token = request.getParameter("token");

                            request.getSession().setAttribute("name", name);
                            request.getSession(true).setAttribute("name", name); // firstway

                            session.setAttribute("name", name); //another way
                            session.setAttribute("age", age);
                            session.setAttribute("gender", gender);
                            session.setAttribute("cat", cat);
                            session.setAttribute("time", time);
                            session.setAttribute("token", token);
                            out.println("Session " + request.getSession().getAttribute("name").toString());
                        } catch (Exception e) {
                            out.println("The exception part is " + e);
                        }

                    %>

                </table>
            </center>

        </fieldset>
    </form>
</body>

Servlet page:

package Patientdetails;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.jms.Session;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class Patientdetails extends HttpServlet {

HttpServletRequest request;
String[] pname = {"name", "age", "gender", "cat", "time", "token"};
String[] pvalue = new String[6];

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try {
        out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<head>");
        // out.println("<title>"+session.getAttribute("name")+"</title>");            
        out.println("</head>");
        out.println("<body>");


    } finally {
        out.close();
    }
}

@Override
public void init() {

}

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

    PrintWriter out = response.getWriter();


    try {
        for (int i = 0; i < pname.length; i++) {
            pvalue[i] = (String) request.getSession().getAttribute(pname[i]);


        }



        out.println("<table  BORDER ='2'><th>Patient Name</th><th>Patient Age</th><th>Gender</th><th>Category of doctor </th><th>Appointment Time</th><th>Token no</th>");

        out.println("<tr><td>" + pvalue[0] + "</td> <td>" + pvalue[1] + "</td><td>" + pvalue[2] + "</td><td>" + pvalue[3] + " </td><td> " + pvalue[4] + " </td><td> " + pvalue[5] + "</td></tr>");
            out.println("<h1>The Session value is " + request.getSession().getAttribute("name")+"</h1>");
        out.println("</body>");
        out.println("</html>");
    } catch (Exception e) {
        out.println("Exception part " + e);
        e.printStackTrace(out);
    }
}

@Override
public void destroy() {
}

}

When i try like this, Iam just getting null value in servlet page.. I don't know why it just passing null values?? Is there any other methods to pass values as session???

Upvotes: 0

Views: 3510

Answers (1)

John Topley
John Topley

Reputation: 115292

You need to keep clear in your mind which code is executing client-side in the browser and which is executing server-side. At the point at which the scriptlet code executes in your JSP, your HTML form hasn't been submitted yet, so there are no form values stored in the request or session objects. That's why the form field values are null.

You should move the code to retrieve the field values from your submitted form to the servlet, as that's where the form submits to (as specified by its action attribute). To get the form values from within your servlet, you can use:

String paramValue = request.getParameter("paramName");

To retrieve a value from the HTTP session, use:

request.getSession().getAttribute("attributeName");

You wouldn't typically retrieve the parameter values from the request for the purposes of storing them in the session. It's cleaner to make the JSP session-scoped to begin with.

As an aside, note that your HTML form should use the HTTP POST method rather than GET if it can't safely be submitted more than once (usually because it's changing some server-side state).

Upvotes: 2

Related Questions