我有一個登錄servlet,它接受來自登錄html文件的值並立即重定向到sessiondemo.html。servlet中的session.getAttribute和html中的顯示
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class logIn extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
HttpSession session = req.getSession(true);
//getting the value(s) from HTML
String fname = req.getParameter("fname");
String lname = req.getParameter("lname");
String email = req.getParameter("email");
String address = req.getParameter("address");
//Storing the values in Session
session.setAttribute("FirstName", fname);
session.setAttribute("LastName", lname);
session.setAttribute("Email", email);
session.setAttribute("address", address);
res.sendRedirect("http://localhost:8080/apps/sessiondemo.html");
out.close();
}
}
我無法弄清楚的是,我想在sessiondemo html中顯示姓和名。我知道代碼必須寫在seesiondemo html中,但它不起作用。
這裏是我的嘗試:
<% session.getAttribute("FirstName", fname);
out.println("You are logged in as" + fname + "!");%>
XML文件
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<description>Test examples</description>
<display-name>My Servlets</display-name>
<servlet>
<servlet-name>GET</servlet-name>
<servlet-class>GET</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GET</servlet-name>
<url-pattern>/GET</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>POST</servlet-name>
<servlet-class>POST</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>POST</servlet-name>
<url-pattern>/POST</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Cart</servlet-name>
<servlet-class>Cart</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Cart</servlet-name>
<url-pattern>/Cart</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>logIn</servlet-name>
<servlet-class>logIn</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>logIn</servlet-name>
<url-pattern>/logIn</url-pattern>
</servlet-mapping>
</web-app>
你爲什麼要嘗試_that_?什麼是'HttpSession#getAttribute(...)'的聲明?它接受了多少個論據? – 2014-10-30 15:32:18
我只是給它一個鏡頭,如果你知道如何在HTML中顯示名字,幫助我? – Reda93 2014-10-30 15:34:49
這表明我沒有做過你的研究。 ['HttpSession#getAttribute(String)'](http://docs.oracle.com/javaee/7/api/javax/servlet/http/HttpSession.html#getAttribute%28java.lang.String%29)只需要一個參數,一個'String'值,不是兩個。 – 2014-10-30 15:42:19