0
我已經創建了醫院管理項目..我想將表單字段值從jsp發送到servlet作爲會話...我嘗試了以下代碼如何將表單字段值作爲會話從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() {
}
}
當我嘗試這樣,Iam只是在Servlet頁面中獲得空值..我不知道它爲什麼只是傳遞空值有沒有其他的方法來傳遞值作爲會議?
爲什麼session.getAttribute(「paramname」)不起作用? – deeban 2014-08-27 10:59:58
但是,getValue()方法已被棄用。它也返回null ... – deeban 2014-08-27 11:00:52
對不起,我的意思是'getAttribute;' – 2014-08-27 11:04:56