日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Struts01---入门小案例

發布時間:2023/12/18 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Struts01---入门小案例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

創建web項目 ? ?實現的效果! 用戶點擊頁面不同的鏈接,后臺調用不同的代碼!

創建兩個類實現共同的接口!

public interface Action { String execute(); }

?

public class LoginAction implements Action{public String execute(){System.out.println("LoginAction......");return "success";} }

?

public class ListAction implements Action {public String execute(){System.out.println("ListAction......");return "success";} }

?

想讓用戶能訪問到我們的后臺代碼,要么使用servlet ?要么使用filter!

使用filter

創建一個filter用來攔截用戶的請求

public class DoFilter implements Filter {//全局的變量Map<String,String> map=new HashMap<String, String>();//初始化操作 @Overridepublic void init(FilterConfig arg0) throws ServletException {System.out.println("DoFilter 初始化了.............................");// key是用戶請求的路徑 value 是對應的全類名map.put("/login","cn.bdqn.action.LoginAction");map.put("/list","cn.bdqn.action.ListAction");}//真正的處理 @Overridepublic void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {//向下轉型HttpServletRequest httpServletRequest=(HttpServletRequest) request;HttpServletResponse httpServletResponse=(HttpServletResponse) response;//看一下 各個路徑的區別System.out.println("getContextPath()==>"+httpServletRequest.getContextPath());//項目名System.out.println("getServletPath()==>"+httpServletRequest.getServletPath());//訪問的路徑System.out.println("getRequestURI()==>"+httpServletRequest.getRequestURI());//項目下面的路徑System.out.println("getRequestURL()==>"+httpServletRequest.getRequestURL());//帶協議的完整路徑//應該使用getServletPathString path=httpServletRequest.getServletPath();try {if (path.equals("/index.jsp")) {chain.doFilter(request, response); //放行}else{Action action=(Action) Class.forName(map.get(path)).newInstance();action.execute();//跳轉到成功界面httpServletRequest.getRequestDispatcher("/success.jsp").forward(request, response);}} catch (InstantiationException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();} catch (ClassNotFoundException e) {e.printStackTrace();}}@Overridepublic void destroy() {}}

?

?

前臺頁面

<%@ 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><a href="login">登錄 </a><a href="list">詳情 </a></body> </html>

?

sucess.jsp頁面就是一個成功界面!!!省略掉!

?

我們使用xml文件來代替 ?map中 所保存的 鍵值對 ?信息!

key:用戶的請求

value:對應的后臺實現類 全類名!

?

轉載于:https://www.cnblogs.com/999-/p/6479202.html

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的Struts01---入门小案例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。