Servlet與JSP 之間的傳值有兩種情況
通過對象 request和 session (不考慮 application)完成傳值
一
JSP頁面有
- <!
JSP page > - <%
- session
setAttribute("testSession" "Hello session"); - reqeust
setAttribute("testRequest" "Hello request"); - %>
- <a href="JspServlet?action=toServlet">點擊</a>
- <form action="JspServlet?action=toServlet" method="post" name="form">
- <input name="username" type="test" />
- <input type="submit" value="submit">
- </form>
二
- <servlet
name>JspServlet </servlet name> - <servlet
class>com demo JspServletDemo</servlet class> - </servlet>
- <servlet
mapping> - <servlet
name>JspServlet </servlet name> - <url
pattern>/JspServlet</url pattern> - </servlet
mapping> - <servlet
name>JspServlet </servlet name> - <servlet
class>com demo JspServletDemo</servlet class> - </servlet>
- <servlet
mapping> - <servlet
name>JspServlet </servlet name> - <url
pattern>/admin/JspServlet</url pattern> - </servlet
mapping>
假如 project name 是 jsp
在 project 根目錄下有 admin目錄
在這兩個目錄下的 jsp 都想轉到 com
url應寫為 "JspServlet"
這裡的 request
session可以認為是每一個IE進程對應一個會話(新開一個IE進程就可以對應兩個會話的)
參數為true (默認)
參數為false
利用這個方法可以判斷 session是否過期
- if(request
getSession(false)==null) - System
out println("Session has been invalidated!"); - else
- System
out println("Session is active!");
三
從 servlet轉到 jsp不外乎兩種方法
- String fr="good well";
- request
setAttribute("test" fr); - RequestDispatcher de=request
getRequestDispatcher("/test jsp"); - de
forward(request response);
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/20260.html