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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

方立勋_30天掌握JavaWeb_request

發布時間:2023/12/20 java 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 方立勋_30天掌握JavaWeb_request 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

request常用方法

import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; //HttpServletRequest的常用方法 http請求中的所有信息都封裝在這個對象中 public class RequestDemo1 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 訪問路徑http://localhost:8080/day06/servlet/RequestDemo1?name=aaa // /day06/servlet/RequestDemo1 System.out.println(request.getRequestURI()); // http://localhost:8080/day06/servlet/RequestDemo1 System.out.println(request.getRequestURL()); // name=aaa System.out.println(request.getQueryString()); // 獲取客戶端ip System.out.println(request.getRemoteAddr()); // 獲取客戶端主機名,這個主機名沒有在DNS上注冊的話還是獲取ip System.out.println(request.getRemoteHost()); // 獲取客戶端瀏覽器的端口 System.out.println(request.getRemotePort()); // 獲取web服務器的ip System.out.println(request.getLocalAddr()); // 獲取web服務器的主機名,沒有在DNS上注冊還是獲取ip System.out.println(request.getLocalName()); // 獲取請求方式 System.out.println(request.getMethod()); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }

request獲取請求頭和請求數據

import java.io.IOException; import java.io.InputStream; import java.lang.reflect.InvocationTargetException; import java.util.Enumeration; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.sun.org.apache.commons.beanutils.BeanUtils; //HttpServletRequest獲取請求頭和請求數據 //請求數據一半來說要先檢查再使用,檢查非空和不是空格 public class RequestDemo2 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("---------獲取請求數據方式1-------------"); // 獲取指定的請求數據 String value = request.getParameter("username"); if (value != null && !value.trim().equals("")) { System.out.println(value); } System.out.println("---------獲取請求數據方式2-------------"); // 獲取所有的請求數據 Enumeration e = request.getParameterNames(); while (e.hasMoreElements()) { String paramName = (String) e.nextElement(); String value2 = request.getParameter(paramName); System.out.println(paramName + "=" + value2); } System.out.println("---------獲取請求數據方式3-------------"); // 獲取所有的請求數據,同名的只能獲取一次,就是第一次 String[] values = request.getParameterValues("username"); for (int i = 0; values != null && i < values.length; i++) { System.out.println(values[i]); } System.out.println("---------獲取請求數據方式4-------------"); // 這個特別實用,框架的模型驅動,這個Map的value肯定是String數組類型,因為有同名的請求數據 // 實際開發中是不會 request.getParameter("username");用這種方式的,都是要創建一個model的 Map<String, String[]> map = request.getParameterMap(); User user = new User(); try { // 用map中的數據填充bean BeanUtils.populate(user, map); } catch (IllegalAccessException e1) { e1.printStackTrace(); } catch (InvocationTargetException e1) { e1.printStackTrace(); } System.out.println(user.getPassword()); System.out.println("---------獲取請求數據方式5-------------"); // request.getInputStream();是上傳文件的時候獲取數據的方式 // 普通數據是獲取不到的 InputStream in = request.getInputStream(); int len = 0; byte[] buffer = new byte[1024]; while ((len = in.read(buffer)) > 0) { System.out.println(new String(buffer, 0, len)); } } // 獲取請求頭 private void test1(HttpServletRequest request) { System.out.println("---------獲取請求頭方式1-------------"); // 拿到指定的請求頭 System.out.println(request.getHeader("cache-control")); System.out.println("---------獲取請求頭方式2-------------"); // 拿到所有指定的請求頭 Enumeration e = request.getHeaders("cache-control"); while (e.hasMoreElements()) { String headValue = (String) e.nextElement(); System.out.println(headValue); } System.out.println("---------獲取請求頭方式3-------------"); // 拿到所有請求頭 Enumeration e1 = request.getHeaderNames(); while (e1.hasMoreElements()) { String headerName = (String) e1.nextElement(); String headValue = request.getHeader(headerName); System.out.println(headerName + "=" + headValue); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } public class User { private String[] username; public String[] getUsername() { return username; } public void setUsername(String[] username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } private String password; }

前臺頁面:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>給RequestDemo2發送請求數據</title> </head> <body> <!-- 瀏覽器可以通過兩種方式向服務器發送請求數據 超鏈接方式后面跟了中文要經過url編碼后再提交 --> <a href="/day06/servlet/RequestDemo2?username=xxx">點點</a><br/> <form action="/day06/servlet/RequestDemo2" method="post"> 用戶名1:<input type="text" name="username"/><br/> 用戶名2:<input type="text" name="username"/><br/> 密碼:<input type="password" name="password"/><br/> <input type="submit" value="提交"/> </form> </body> </html>

注:此程序還需用到commons-beanutils-1.9.0.jar和commons-logging-1.1.3.jar這兩個jar包。

通過表單提交用戶數據和用request獲取表單提交數據

表單頁面:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>收集用戶數據,向RequestDemo3提交數據</title> </head> <body> <form action="/day06/servlet/RequestDemo3" method="post"> 用戶名:<input type="text" name="username" /> <br /> 密碼:<input type="password" name="password" /> <br /> 性別: <input type="radio" name="gender" value="male" /><input type="radio" name="gender" value="female" /><br /> 所在地: <select name="city"> <option value="beijing">北京</option> <option value="shanghai">上海</option> <option value="shenzhen">深圳</option> </select> <br /> 愛好: <input type="checkbox" name="likes" value="sing" />唱歌 <input type="checkbox" name="likes" value="dance" />跳舞 <input type="checkbox" name="likes" value="basketball" />籃球 <input type="checkbox" name="likes" value="football" />足球 <br /> 簡介: <textarea rows="6" cols="60" name="description"></textarea> <br /> <input type="hidden" name="id" value="123456"/> <input type="submit" value="提交" /> </form> </body> </html> import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.beanutils.BeanUtils; //獲取表單提交數據 public class RequestDemo3 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { test2(request); } // 實際開發項目時采用的model方式 private void test2(HttpServletRequest request) { User user = new User(); Map<String, String[]> map = request.getParameterMap(); try { BeanUtils.populate(user, map); System.out.println(user.getUsername()); System.out.println(user.getPassword()); System.out.println(user.getGender()); System.out.println(user.getCity()); String[] likes = user.getLikes(); for (int i = 0; likes != null && i < likes.length; i++) { System.out.println(likes[i]); } System.out.println(user.getDescription()); System.out.println(user.getId()); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } // 直接獲取值得方法 private void test1(HttpServletRequest request) { String username = request.getParameter("username"); System.out.println(username); String password = request.getParameter("password"); System.out.println(password); String gender = request.getParameter("gender"); System.out.println(gender); String city = request.getParameter("city"); System.out.println(city); String[] likes = request.getParameterValues("likes"); for (int i = 0; likes != null && i < likes.length; i++) { System.out.println(likes[i]); } String description = request.getParameter("description"); System.out.println(description); String id = request.getParameter("id"); System.out.println(id); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } public class User { private String username; private String password; private String gender; private String city; private String[] likes; private String description; private String id; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String[] getLikes() { return likes; } public void setLikes(String[] likes) { this.likes = likes; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getId() { return id; } public void setId(String id) { this.id = id; } }

request亂碼問題(數據提交以post方式和get方式)

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>向RequestDemo4提交中文數據,解決亂碼問題</title> </head> <body> <!--post方式提交--> <form action="/day06/servlet/RequestDemo4" method="post"> 用戶名:<input type="text" name="username" /> <input type="submit" value="提交" /> </form> <!--get方式提交--> <form action="/day06/servlet/RequestDemo4" method="get"> 用戶名:<input type="text" name="username" /> <input type="submit" value="提交" /> </form> <!-- 超鏈接方式提交的中文,服務器想不亂碼,也只能手工處理(貌似超鏈接提交要經過url編碼)--> <a href="/day06/servlet/RequestDemo4?username=中國">單擊</a> </body> </html> import java.io.IOException; import java.io.UnsupportedEncodingException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; //request亂碼解決,除了下面的解決方式,還可以改服務器配置,但不要用那種方式 public class RequestDemo4 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { test2(request); } /* * get方式提交:由于get方式提交request.setCharacterEncoding(arg0);無效,所以亂碼只能手工處理,拿到亂碼的數據后 * ,按照ISO8859-1進行解碼,然后按照UTF-8進行編碼。 */ private void test2(HttpServletRequest request) throws UnsupportedEncodingException { String username = request.getParameter("username"); // get方式解決亂碼,先把亂碼按照原來的編碼解碼,返回數據的表示數字,然后按照想要的碼表編碼 username = new String(username.getBytes("ISO8859-1"), "UTF-8"); System.out.println(username); } /* * post方式提交:瀏覽器以當前頁面的碼表提交數據,當前頁面的碼表是程序員寫程序時自己指定的。數據提交到request里面去, * 但是當request往外面取數據時用的是ISO8859 * -1碼表,就會產生亂碼所以要在取數據之前指定request的碼表。request.setCharacterEncoding * (arg0);只對post方式起作用 */ private void test1(HttpServletRequest request) throws UnsupportedEncodingException { // 設置request碼表 request.setCharacterEncoding("UTF-8"); String username = request.getParameter("username"); System.out.println(username); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }

request實現請求轉發以及request域帶數據給轉發資源

MVC思想:

M model javabean
V view(jsp)
C Cotroller(servlet)
servlet把數據提交,javabean封裝數據,然后jsp從javabean取出數據。

請求轉發特點:
1. 客戶端只發一次請求,而服務器有多個資源調用
2. 瀏覽器地址欄沒有變化

import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; //request請求轉發,以及使用request域把數據帶給轉發資源,實際開發中MVC設計模式,都是用request域把數據帶給jsp的 public class RequestDemo5 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String date = "aaaaaa"; // 將數據存到request域中 request.setAttribute("date", date); // 請求轉發,servletContext也可以實現請求轉發 request.getRequestDispatcher("/message.jsp").forward(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body> ${date} <!-- 實際開發中 是不允許jsp頁面中出現java代碼的,都是用自定義標簽和EL表達式替換java代碼 --> <% //request的getParameter方法得到的是請求數據,getAttribute方法得到的是request域里的數據 String message = (String)request.getAttribute("date"); out.write(message); %> </body> </html>

request請求轉發forward方法的細節

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; //request請求轉發時forward方法的細節,客戶端只發一次請求,而服務器端有多個資源調用,地址欄不會發生變化 /* * 1. forward方法用于將請求轉發到RequestDispatcher對象封裝的資源, * 2. 如果在調用forward方法之前,在servlet程序中寫入的部分內容已經被真正的傳送到了客戶端(調用了flush或者close方法), * forward方法將拋出IllegalStateException異常 * 3. 如果在調用forward方法之前向Servlet引擎的緩沖區(response)中寫入了內容, * 只要寫入到緩沖區的內容還沒有被真正輸出到客戶端,forward方法就可以被正常執行, * 原來寫入到緩沖區中的內容將被清空,但是,以寫入到HttpServletResponse對象中的響應頭字段信息保持有效 */ public class RequestDemo6 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { test3(request, response); } //細節3 private void test3(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String date = "aaaaaa"; PrintWriter print = response.getWriter(); print.write(date); //以下代碼不會出現問題,但是上面的date是不能在頁面輸出的,頁面中只能看見index.jsp的內容 request.getRequestDispatcher("/index.jsp").forward(request, response); } //細節2,轉發了好多次,避免這個問題,forward之后要return private void test2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String date = "aaaaaa"; if(true) { //幾百行代碼 request.getRequestDispatcher("/index.jsp").forward(request, response); //return; //跳轉之后一定要記得return } //幾百行代碼 // 以下代碼將出現:java.lang.IllegalStateException: Cannot forward after response has been committed //上面已經轉發一次了 request.getRequestDispatcher("/index.jsp").forward(request, response); } //細節2 private void test1(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String date = "aaaaaa"; PrintWriter print = response.getWriter(); print.write(date); print.flush();//print.close(); // 以下代碼將出現:java.lang.IllegalStateException: Cannot forward after response has been committed request.getRequestDispatcher("/index.jsp").forward(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }

request使用RequestDispatcher的include方法實現頁面包含

import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; //request使用RequestDispatcher的include方法實現頁面包含 public class RequestDemo7 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.getRequestDispatcher("/head.jsp").include(request, response); response.getWriter().write("aaaaaa"); request.getRequestDispatcher("/foot.jsp").include(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }

工程中各類地址的寫法

import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; //web工程中各類地址的寫法 //寫地址最好以/開頭,如果是給服務器用的,/代表當前的web應用;如果地址是給瀏覽器用的,/代表網站(webapps文件夾) //相對路徑的話單獨問題單獨解決 public class ServletUrlDemo extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 1.給服務器用 request.getRequestDispatcher("/").forward(request, response); // 2.給瀏覽器用,讓瀏覽器重定向 response.sendRedirect("/"); // 3.給服務器用 this.getServletContext().getRealPath("/"); // 4.給服務器用 this.getServletContext().getResourceAsStream("/"); // 5. /* * //給瀏覽器用的 <a href="/">點擊</a> //給瀏覽器用的 <form action="/"> * * </form> */ // 要想獲取url資源用/這個斜杠,獲取硬盤上的資源,用\\斜杠 } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }

request獲取referer請求頭實現防盜鏈

eg:有的資源你點出后會有廣告,廣告旁邊是資源連接,有些人直接把資源連接發給別人,企圖不看廣告直接進入鏈接拿資源,為了防止盜鏈行為的發生,我們要檢測用戶訪問url的情況來進行一系列措施。
需要實現的功能就是,當用戶想要查看”機密文檔”的時候,如果是直接輸入機密文檔的url,而不是廣告的url,我們得先讓他跳轉到廣告頁面的url,看完廣告后就可以讓他看“機密文檔”了。
模擬過程:用戶輸入機密文件的url(或者在其他網站),這時候進入Servlet,response的getHeader(“referer”)方法會得到來訪地址,用此判斷是否是從index.jsp網頁的url來的,如果不是,跳入帶廣告的index.jsp,如果是就把機密文件的內容加載,然后顯示給用戶。

import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; //利用referer請求頭實現防盜鏈 public class RequestDemo8 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 獲取請求是從哪里來的 String referer = request.getHeader("referer"); // 如果是直接輸入的地址,或者不是從本網站訪問的重定向到本網站的首頁 if (referer == null || !referer.startsWith("http://localhost")) { response.sendRedirect("/day06/index.jsp"); // 然后return,不要輸出后面的內容了 return; } String date = "鳳姐日記"; response.getWriter().write(date); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }

總結

以上是生活随笔為你收集整理的方立勋_30天掌握JavaWeb_request的全部內容,希望文章能夠幫你解決所遇到的問題。

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