如何将表单字段值作为会话从jsp发送到servlet

时间:2014-08-27 10:47:04

标签: jsp servlet-3.0

我已经为医院管理创建了项目..我想将表单字段值从jsp发送到servlet作为会话...我试过以下代码

Jsp页面:

 <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页面:

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() {
}

}

当我尝试这样的时候,我只是在servlet页面中获取null值。我不知道为什么它只是传递空值?有没有其他方法可以将值传递为session ???

1 个答案:

答案 0 :(得分:1)

您需要清楚地了解哪些代码在浏览器中执行客户端,哪些代码正在执行服务器端。在脚本代码在JSP中执行的时刻,您的HTML表单尚未提交,因此requestsession对象中没有存储任何表单值。这就是表单字段值为null的原因。

您应该移动代码以将提交的表单中的字段值检索到servlet,就像表单提交的那样(由action属性指定)。要从servlet中获取表单值,可以使用:

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

要从HTTP会话中检索值,请使用:

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

您通常不会从请求中检索参数值,以便将它们存储在会话中。让JSP会话作为范围开始更清晰。

顺便说一句,请注意您的HTML表单应该使用HTTP POST方法而不是GET,如果它不能安全地多次提交(通常是因为它改变了某些服务器端状态)。