掃二維碼與項(xiàng)目經(jīng)理溝通
我們?cè)谖⑿派?4小時(shí)期待你的聲音
解答本文疑問(wèn)/技術(shù)咨詢(xún)/運(yùn)營(yíng)咨詢(xún)/技術(shù)建議/互聯(lián)網(wǎng)交流
[[390002]]

創(chuàng)新互聯(lián)建站是一家專(zhuān)業(yè)提供滿(mǎn)城企業(yè)網(wǎng)站建設(shè),專(zhuān)注與成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站、H5頁(yè)面制作、小程序制作等業(yè)務(wù)。10年已為滿(mǎn)城眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專(zhuān)業(yè)網(wǎng)絡(luò)公司優(yōu)惠進(jìn)行中。
本文轉(zhuǎn)載自微信公眾號(hào)「見(jiàn)賢思編程」,作者泰斗賢若如。轉(zhuǎn)載本文請(qǐng)聯(lián)系見(jiàn)賢思編程公眾號(hào)。
轉(zhuǎn)發(fā)與重定向簡(jiǎn)介
轉(zhuǎn)發(fā)和重定向都是實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)
也就是說(shuō),當(dāng)我們?cè)L問(wèn)一個(gè) Servlet 的時(shí)候 ,Servlet 幫我們跳轉(zhuǎn)到另一個(gè)界面。
轉(zhuǎn)發(fā)與重定向的區(qū)別
代碼演示轉(zhuǎn)發(fā)和重定向
- package servlet;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
- @WebServlet("/login")
- public class ServletDemo extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- //獲取表單提交過(guò)來(lái)的數(shù)據(jù)
- //getParameter()方法可以獲取請(qǐng)求的參數(shù)信息
- String name = req.getParameter("name");
- String password = req.getParameter("password");
- //打印獲取到的參數(shù)信息
- System.out.println("name:"+name);
- System.out.println("password:"+password);
- //如果name=admin,password=123,則跳轉(zhuǎn)到succee.jsp,否則跳轉(zhuǎn)到fail.jsp
- if("admin".equals(name)&&"123".equals(password)){
- //通過(guò)轉(zhuǎn)發(fā)實(shí)現(xiàn)跳轉(zhuǎn)
- req.getRequestDispatcher("/success.jsp").forward(req,resp);
- }else {
- //通過(guò)重定向?qū)崿F(xiàn)跳轉(zhuǎn)
- resp.sendRedirect("/fail.jsp");
- }
- }
- @Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- doGet(req, resp);
- }
- }
JSP代碼
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
登錄
賬號(hào): 密碼:
轉(zhuǎn)發(fā)和重定向如何帶數(shù)據(jù)到某個(gè)頁(yè)面
- package servlet;
- import javax.servlet.ServletContext;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
- @WebServlet("/login")
- public class ServletDemo extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- //通過(guò)轉(zhuǎn)發(fā)帶數(shù)據(jù)
- req.setAttribute("name","張三");
- req.getRequestDispatcher("/send.jsp").forward(req,resp);
- }
- @Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- doGet(req, resp);
- }
- }
send.jsp
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
轉(zhuǎn)發(fā)和重定向傳代數(shù)據(jù)練習(xí) - <%
- //1、接收轉(zhuǎn)發(fā)傳代的數(shù)據(jù)
- String name = (String) request.getAttribute("name");
- out.println("轉(zhuǎn)發(fā)傳代的數(shù)據(jù):"+name);
- %>
- package servlet;
- import javax.servlet.ServletContext;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
- @WebServlet("/login")
- public class ServletDemo extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- //通過(guò)重定向帶數(shù)據(jù)
- ServletContext servletContext = this.getServletContext();
- servletContext.setAttribute("name","王二麻子");
- resp.sendRedirect("/send2.jsp");
- }
- @Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- doGet(req, resp);
- }
- }
send2.jsp
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
轉(zhuǎn)發(fā)和重定向傳代數(shù)據(jù)練習(xí) - <%
- //1、接收重定向傳代的數(shù)據(jù)
- String name1 = (String)application.getAttribute("name");
- out.println("重定向傳代的數(shù)據(jù):"+name1);
- %>
練習(xí)
index.jsp
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title 加法計(jì)算器
- 加數(shù)1:
- 加數(shù)2:
count.jsp
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title - 計(jì)算結(jié)果:<%=request.getAttribute("count")%>
Servlet
- package servlet;
- import javax.servlet.ServletContext;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
- @WebServlet("/CountServlet")
- public class CountServlet extends HttpServlet {
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- String one=request.getParameter("one");
- int o=Integer.parseInt(one);//強(qiáng)制轉(zhuǎn)換,將String類(lèi)型的數(shù)據(jù)轉(zhuǎn)換成int類(lèi)型
- String two=request.getParameter("two");
- int t=Integer.parseInt(two);//強(qiáng)制轉(zhuǎn)換,將String類(lèi)型的數(shù)據(jù)轉(zhuǎn)換成int類(lèi)型
- System.out.println(one+" "+two);
- int c=o+t;
- String co=String.valueOf(c);//將int類(lèi)型的數(shù)據(jù)轉(zhuǎn)換成String類(lèi)型
- //轉(zhuǎn)發(fā),可以攜帶數(shù)據(jù)
- request.setAttribute("count",co);
- request.getRequestDispatcher("count.jsp").forward(request,response);
- //用于存放數(shù)據(jù)
- // ServletContext s=this.getServletContext();
- // s.setAttribute("count",co);
- //重定向只能依靠ServletContext獲取數(shù)據(jù)
- // response.sendRedirect("count.jsp");
- System.out.println(co);
- }
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- doPost(request,response);
- }
- }

我們?cè)谖⑿派?4小時(shí)期待你的聲音
解答本文疑問(wèn)/技術(shù)咨詢(xún)/運(yùn)營(yíng)咨詢(xún)/技術(shù)建議/互聯(lián)網(wǎng)交流