struts2在Action中访问Session,管理员删除用户
生活随笔
收集整理的這篇文章主要介紹了
struts2在Action中访问Session,管理员删除用户
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
結(jié)構(gòu)如下:
當沒有登錄admin用戶去刪除時:
當?shù)卿沘dmin用戶刪除時:
首先是一個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}}UserDeleteAction.java
package my;import java.util.Map;import org.apache.log4j.Logger; import org.apache.struts2.interceptor.SessionAware;import com.opensymphony.xwork2.ActionSupport;public class UserDeleteAction extends ActionSupportimplements SessionAware{static Logger logger=Logger.getLogger(UserDeleteAction.class);int id=-1; //要刪除的idString reason; //結(jié)果private Map<String,Object> userSession;@Overridepublic void setSession(Map<String, Object> arg0) {userSession=arg0;}@Overridepublic String execute() throws Exception {String user=(String)userSession.get("user");if(!"admin".equals(user)){reason="無權(quán)限進行刪除!";return "error";}if(id<=0){reason="請輸入刪除的ID!";return "error";}return "success";}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getReason() {return reason;}public void setReason(String reason) {this.reason = reason;}}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>UserDelete_error.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 'UserDelete_error.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><h3>操作失敗</h3><p><s:property value="reason"/></p></body> </html>UserDelete_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 'UserDelete_success.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>操作成功。用戶 [ <s:property value="id" /> ] 被刪除!</body> </html>總結(jié)
以上是生活随笔為你收集整理的struts2在Action中访问Session,管理员删除用户的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux cpu load 详解,理解
- 下一篇: php符号 set,PHP 符号大全