Struts2 访问上下问对象
生活随笔
收集整理的這篇文章主要介紹了
Struts2 访问上下问对象
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
MyEclipse結構如下:
在my包下有2個類,
一個是Servlet是LoginServlet.java
package my;import java.io.IOException; import java.io.PrintWriter;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 LoginServlet extends HttpServlet {/*** Constructor of the object.*/public LoginServlet() {super();}/*** Destruction of the servlet. <br>*/public void destroy() {super.destroy(); // Just puts "destroy" string in log// Put your code here}/*** The doGet method of the servlet. <br>** This method is called when a form has its tag value method equals to get.* * @param request the request send by the client to the server* @param response the response send by the server to the client* @throws ServletException if an error occurred* @throws IOException if an error occurred*/public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html");PrintWriter out = response.getWriter();out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");out.println("<HTML>");out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");out.println(" <BODY>");out.print(" This is ");out.print(this.getClass());out.println(", using the GET method");out.println(" </BODY>");out.println("</HTML>");out.flush();out.close();}/*** The doPost method of the servlet. <br>** This method is called when a form has its tag value method equals to post.* * @param request the request send by the client to the server* @param response the response send by the server to the client* @throws ServletException if an error occurred* @throws IOException if an error occurred*/public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {request.setCharacterEncoding("UTF-8");String user=request.getParameter("user");String password=request.getParameter("password");if("123456".equals(password)){HttpSession hs=request.getSession();hs.setAttribute("user", user);//重定向到index.jspString contextPath=request.getContextPath();response.sendRedirect(contextPath+"/index.jsp");}else{response.sendError(400, "密碼錯誤!");}}/*** Initialization of the servlet. <br>** @throws ServletException if an error occurs*/public void init() throws ServletException {// Put your code here}}另外一個是Test2Action.java
package my;import com.opensymphony.xwork2.ActionSupport;public class Test2Action extends ActionSupport {String reason = "A test message!";int result = 0;@Overridepublic String execute() throws Exception{result = 100; return "success";}public String getReason(){return reason;}public void setReason(String reason){this.reason = reason;}public int getResult(){return result;}public void setResult(int result){this.result = result;} }struts.xml配置如下
index.jsp配置
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><base href="<%=basePath%>"><title>My JSP 'index.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body>首頁</body> </html>login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><base href="<%=basePath%>"><title>My JSP 'login.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body><form method="post" action="servlet/LoginServlet">用戶名:<input type="text" name="user"><br><br>密碼:<input type="password" name="password"><br><br><input type="submit" name="登錄"><br><br></form></body> </html>Test2_success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %><% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><base href="<%=basePath%>"><title>My JSP 'Hello.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body><h1> 測試1 : #parameters </h1>( 相當于 request.getParameter("") ) <br>result: <s:property value="#parameters['check']" /><h1> 測試2 : #session </h1>( 相當于 session.getAttribute("") ) <br>result: <s:property value="#session['user']" /> <br>另外一種寫法 : <s:property value="#session.user" /><h1> 測試3 : #reqeust</h1>#request['xxx'] 相當于 request.getAttribute("xxx") <br>#parameters['xxx'] 相當于 request.getParameter("xxx") <br>兩者不同<h1> 測試4 :#action </h1>result: <s:property value="#action['reason']" /> <br>相當于: <s:property value="reason" /><h1> 測試5 :if判斷 </h1><s:if test="result > 100" >夠大!</s:if><s:else>太小了!</s:else><h1> 測試6 :if判斷 </h1><s:if test="#session['user'] == null" >未登錄</s:if><s:else><s:property value="#session['user']" />,您好!</s:else></body> </html>總結
以上是生活随笔為你收集整理的Struts2 访问上下问对象的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java基础入门笔记-链表与容器
- 下一篇: Linux 禁用msi模式,禁用MSI模