Request转发---应用
生活随笔
收集整理的這篇文章主要介紹了
Request转发---应用
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Request轉(zhuǎn)發(fā)
- HttpServletRequest 代表客戶端的請求 ,用戶通過Http協(xié)議訪問服務(wù)器
HTTP 請求中的所有信息會被封裝到HttpServletRequest 通過這個HttpServletRequest 可以獲得所有信息
獲取前端傳遞的參數(shù)
req.getParameter(string s) string req.getParameterValues(string s) string[] //第二行是獲取多選框的數(shù)據(jù) 或者是一些其他多選的的數(shù)據(jù)測試類代碼
package com.hui.servlet;import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.Arrays;public class RequesetTest extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {req.setCharacterEncoding("utf-8");// 這里是處理輸出時的亂碼String username = req.getParameter("username");String password = req.getParameter("password");String[] hobbys = req.getParameterValues("hobbys");//獲取前端的數(shù)據(jù) System.out.println("=============");System.out.println(username);System.out.println(password);System.out.println(Arrays.toString(hobbys));//這里是打印出前端輸入的數(shù)據(jù) 以便于檢查 還有后續(xù)學(xué)習(xí) 的 判斷等等 還有一些處理System.out.println("==============");//這是通過請求轉(zhuǎn)發(fā)req.getRequestDispatcher("/success.jsp").forward(req,resp);req.setCharacterEncoding("utf-8");// 這里是 處理輸出時的亂碼問題}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);} }JSP代碼
<%--這是寫的那個登錄頁面--%> <html> <body> <h2>登錄</h2> <%--這里提交的路徑, 需要找到項目的路徑--%> <%--${pageContext.request.contextPath}當(dāng)前項目路徑--%> <%--這里表單表示的意思: 以post方式提交表單提交到login2請求--%> <form action="${pageContext.request.contextPath}/login2" method="post">用戶名:<input type="text" name="username"><br>密碼:<input type="password" name="password"><br>愛好:<!-- 多選框 --><input type="checkbox" name="hobbys"value=" 女孩" >女孩<input type="checkbox" name="hobbys" value=" 代碼"> 代碼<input type="checkbox" name="hobbys" value=" 唱歌"> 唱歌<input type="checkbox" name="hobbys" value=" 電影"> 電影<input type="checkbox" name="hobbys" value=" 騎行">騎行<br><input type="submit"> </form> </body> </html>登錄成功顯示的頁面
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>Title</title> </head> <body> <h1>登錄成功</h1> </body> </html>-
登錄的頁面
-
輸入數(shù)據(jù)之后 IDEA和頁面的變化
-
以后的學(xué)習(xí)都會逐漸跟進(jìn) 希望大家也一起用博客記錄自己的成長
-
加油!!!
總結(jié)
以上是生活随笔為你收集整理的Request转发---应用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Response重定向---javawe
- 下一篇: 二级菜单--竖排---HTML