Java-Servlet跳转Servlet以及传参

最近项目中有这么一个需求:

JSP中的按钮点击------->Servlet-1--------->Servlet-2作出判断并传参------JSP页面接收参数并展示

JSP跳转Servlet

login.jsp button2跳转到ToRegisterViewServlet

 
<--此处的action跳转到对应的LoginServlet是通过button为submit提交触发的-->
<--这是登录按钮-->   <--这是注册铵钮需要跳转到另一个Servlet-->

ToRegisterViewServlet

package net.ptcs.my12306.controller;

import java.io.IOException;

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 net.ptcs.my12306.service.ProvinceService;

/**
 * Servlet implementation class ToRegisterViewServlet
 */
@WebServlet(description = "去往注册页面的servlet", urlPatterns = { "/ToRegisterViewServlet" })
public class ToRegisterViewServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        //把所有省份数据传给页面
        request.setAttribute("provinces", ProvinceService.getInstance().getAllProvince());
        request.getRequestDispatcher("/user_register.jsp").forward(request, response);
    }

}

user_register.jsp


核心代码

UserServlet

package net.ptcs.my12306.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

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 net.ptcs.my12306.entity.Users;
import net.ptcs.my12306.service.UserService;

/**
 * Servlet implementation class UserServlet
 */
@WebServlet(description = "", urlPatterns = { "/UserServlet" })
public class UserServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        
        //1.获取数据
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        String confirm_password=request.getParameter("confirm_password");
        String sex=request.getParameter("sex");
        String birthday_date=request.getParameter("birthday");
        
        //2.数据的非空校验和合法性校验
        StringBuffer sb = validateRegisterForm(username, password, confirm_password);
        
        if(sb.length() > 0) {
            //如果校验不通过,那么返回注册页面,让用户再注册一次
            request.setAttribute("message", "必填信息为空,请重新注册");
            request.getRequestDispatcher("/ToRegisterViewServlet").forward(request, response);
        }else {
            //3.调用底层service的注册方法添加用户到数据库
            Date birthday=null;
            try {
                birthday=new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("birthday"));
            } catch (Exception e) {
                e.printStackTrace();
            }
            
            UserService userService=UserService.getInstance();
            
            Users user = new Users(request.getParameter("username"), request.getParameter("password"), 
                    request.getParameter("sex").charAt(0), birthday);
            user.setLoginIp(request.getRemoteAddr());
            //服务端校验通过之后,注册方法调用之前,应该先判断用户名是否已经存在
            /*
             * 则需要定义判断用户名是否已经存在的方法,如果存在则返回注册页面,提示用户名已经存在,
             *                               如果不存在则继续注册
             */

            if(userService.isExistsUserName(username))
            {
                //用户名已经存在,回到注册页面
                request.setAttribute("message", "用户名已被占用");
                request.getRequestDispatcher("/ToRegisterViewServlet").forward(request, response);
        
            }else
            {

                if(userService.register(user))
                {
                    //生产环境不用挨骂的代码:需求,既要有弹窗又要重定向登录页面
                    
                    response.sendRedirect(request.getContextPath()+"/login.jsp?message=registersuccess");
                }else
                {
                    //注册失败,回到注册页面
                    request.setAttribute("message", "注册失败");
                    request.getRequestDispatcher("/ToRegisterViewServlet").forward(request, response);
                }
            }
        }
    }
    /**
     * 对表单进行服务端校验的方法 
     * @param username
     * @param password
     * @param confirm_password
     * @return 
     */
    private StringBuffer validateRegisterForm(String username, String password,
            String confirm_password) {
        StringBuffer validate_message=new StringBuffer();
        if(username==null||"".equals(username))
        {
            validate_message.append("用户名为空");
        }
if(password==null||"".equals(password)||confirm_password==null||"".equals(confirm_password))
        {
            validate_message.append("密码或者确认密码为空");
        }
        if(!password.equals(confirm_password))
        {
            validate_message.append("两次密码输入不一致");
        }
        if(validate_message.length()>0)
        {
            System.out.println(validate_message.toString());
            return validate_message;
        }
        return validate_message;
    }
}

因此处设置的键是message而UserServlet是跳转到 ToRegisterViewServlet 而
ToRegisterViewServlet 是跳转到 user_register.jsp

user_register.jsp接收参数

  1. jsp页面中写java代码

<%=request.getAttribute("message")==null?"":request.getAttribute("message") %> 
  1. EL表达式
${message}

拓展:
EL表达式${message}默认从请求域拿数据
第一个是request
第二个是session
第三个是Application
并且EL表达式会默认进行非空判断

效果如下:

分割线


博主为咯学编程:父母不同意学编程,现已断绝关系;恋人不同意学编程,现已分手;亲戚不同意学编程,现已断绝来往;老板不同意学编程,现已失业三十年。。。。。。如果此博文有帮到你欢迎打赏,金额不限。。。

你可能感兴趣的:(Java-Servlet跳转Servlet以及传参)