java web-转发、重定向

Hello friends social networking, on this occasion the admin wants to share an article entitled java web-转发、重定向 , we have made good, quality and useful articles for you to read and take information in. hopefully the post content is about which we write you can understand. Alright, happy reading.

java web-转发、重定向

这里用简单的例子分别实现转发和重定向,然后对其进行比较。

1、转发(forward)

首先在简单修改下index.jsp以实现最基本的登录功能:
jsp
接下来编写servlet接收该请求并执行简单的登录校验:
package com.zx.demo;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String loginName = req.getParameter("loginName");
String password = req.getParameter("password");

 if (loginName == null || loginName.trim().equals("")) {  resp.getWriter().write("username must not null");  return; } if (password == null || password.trim().equals("")) {  resp.getWriter().write("password must not null");  return; } if (loginName.equals("admin") && password.equals("123456")) {  req.getRequestDispatcher("helloworld").forward(req, resp); } else {  resp.getWriter().write("username or password wrong"); }}

}


运行程序,输入正确账号密码,请求会被转发到helloworld servlet,转发不会改变请求url和参数,结果如下:

jsp


2、重定向(redirect)

延用上述jsp,修改servlet代码实现重定向,重定向url既可以是本容器,也可以是外部地址:
package com.zx.demo;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String loginName = req.getParameter("loginName");
String password = req.getParameter("password");

 if (loginName == null || loginName.trim().equals("")) {  resp.getWriter().write("username must not null");  return; } if (password == null || password.trim().equals("")) {  resp.getWriter().write("password must not null");  return; } if (loginName.equals("admin") && password.equals("123456"......

原文转载:http://www.shaoqun.com/a/898544.html

跨境电商:https://www.ikjzd.com/

mav:https://www.ikjzd.com/w/2414

扬帆计划:https://www.ikjzd.com/w/1581

skyee:https://www.ikjzd.com/w/290


javaweb-转发、重定向这里用简单的例子分别实现转发和重定向,然后对其进行比较。1、转发(forward)首先在简单修改下index.jsp以实现最基本的登录功能:接下来编写servlet接收该请求并执行简单的登录校验:packagecom.zx.demo;importjakarta.servlet.ServletException;importjakarta.servlet.http.Htt
tiki:https://www.ikjzd.com/w/2053
兴宁玖崇湖温泉怎么样啊?简介一下好不好:http://www.30bags.com/a/225664.html
兴宁七夕哪里有活动?兴宁七夕适合约会的好地方?:http://www.30bags.com/a/431755.html
兴宁熙和湾水上乐园必须穿泳衣吗?熙和湾水上乐园泳衣有卖的吗?:http://www.30bags.com/a/431697.html
兴宁熙和湾音乐节有什么好玩的?2021熙和湾音乐节活动安排?:http://www.30bags.com/a/431731.html
半夜洗干净请你吃鲍鱼 宝贝你的扇贝夹到我了:http://lady.shaoqun.com/a/247496.html
我和老头做了好大好爽 七旬老汉一夜要了我5次:http://lady.shaoqun.com/m/a/248165.html
我在做饭他在下添 男朋友喜欢㖭我全过程:http://www.30bags.com/m/a/249946.html
排卵不算?月经期结束后多少天在同一个房间容易怀孕?医生马上就说清楚了:http://lady.shaoqun.com/a/436485.html
冲出亚马逊!鸿星尔克登上新品榜第一名:https://www.ikjzd.com/articles/146952
上半年国内出口排名出炉,看看你省排第几!:https://www.ikjzd.com/articles/146954
亚马逊B2B业务是如何运作的?:https://www.ikjzd.com/articles/146953


That's the article: java web-转发、重定向
Thank you for visiting my blog, hopefully it can be useful for all of you. Don't forget to share this article with your friends so they also know the interesting info, see you in other article posts.

You are now reading the article java web-转发、重定向 with link address https://socialnetworkingupdate.blogspot.com/2021/07/java-web.html

More Articles

Post a Comment

Mas Bago Mas luma

Iklan In-Feed (homepage)

#Advertisement