java—我应该如何将参数从jsp页发送到servlet,然后发送到jsp页,然后再发送到servlet?

rekjcdws  于 2021-07-09  发布在  Java
关注(0)|答案(0)|浏览(345)

我正在做一个登录表单。并希望 changepass.java 文件。
应用程序的流程是:login.jsp->loginservlet->afterlogin.jsp->changepass.jsp->changepasswordservlet
我试着用 session 以及 request dispatcher 为此,它是不起作用的。
我可以从login.jsp获取参数 ChangePass.jsp 但在那之后 changePassword servlet无法访问它。
我的流程代码是:
登录.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<body>
<div align="center">
<form name="login" onsubmit="return onSubmit()" action="LoginServlet">
<h2  style="color:blue">Account Login</h2>
<table style="background-color:powderblue;border: 2px solid green;border-color:green;">
<tr><td>Enter User Name:</td><td><input type="text" name="username" minlength="6" required></td></tr>
<tr><td>Enter Pass Word:</td><td><input type="password" name="password" id="password" required></td></tr>
<tr><td colspan="2" align="center"><input type="submit" value="login"></td></tr>
</table>
<br>
<a href="FirstPage.html" class="btn btn-primary"><u>Home</u></a>
</form>
</div>
<%
String msg=(String) request.getAttribute("msg");
if(msg!=null){
%>
<p style="color:red"><%=msg%></p>
<%  
}
%>
<script type="text/javascript">
function onSubmit(){
    var y=document.login.password.value;
    var passR=/^(?=.*\d)(?=.*[A-Z]).{6,20}$/;
    if(!y.match(passR)){
        alert("Password must contain at least one number and one UpperCase letter");
        document.getElementById("password").focus();
    }
}
</script>
</body>
</html>

loginservlet.java文件

package com.wipro.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

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 javax.servlet.http.HttpSession;

import com.wipro.util.DButil;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        try{
        Connection con=DButil.getConnection();
        PrintWriter out=response.getWriter();

        String username=request.getParameter("username");
        String password=request.getParameter("password");
        String sql="select * from user_table where username=? and password=?";
        PreparedStatement st=con.prepareStatement(sql);
        st.setString(1, username);
        st.setString(2, password);
        ResultSet rs=st.executeQuery();

        if(rs.next()) {
            HttpSession se=request.getSession();
            se.setAttribute("username", username );
            request.getRequestDispatcher("AfterLogin.jsp").include(request, response);
        }
        else {
            request.setAttribute("msg", "Invalid Credentials");
            request.getRequestDispatcher("login.jsp").include(request, response);
        }
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

登录后.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1><font color=green> Welcome <%=session.getAttribute("username")%></font></h1>
<a href="ChangePass.jsp">Change Password</a>
</body>
</html>

更改传递.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<div align="center">
<form name="changePS" onsubmit="return onSubmit()" action="ChangePassword">
<%
String username=(String)session.getAttribute("username");
request.setAttribute("username", username);
%>
<h1><font color=green> Welcome <%=username%></font></h1>
<h2  style="color:blue">Change Password</h2>
<table style="background-color:powderblue;border: 2px solid green;border-color:green;">
<tr><td>Old Password:</td><td><input type="password" name="oldpwd" id="oldpwd" required></td></tr>
<tr><td>New Password:</td><td><input type="password" name="newpwd" id="newpwd" required></td></tr>
<tr><td>Confirm Password:</td><td><input type="password" name="confpwd" id="confpwd" required></td></tr>
<tr><td colspan="2" align="center"><input type="submit" value="login"></td></tr>
</table>
<br>
<a href="FirstPage.html" class="btn btn-primary"><u>Home</u></a>
</form>
</div>
<script type="text/javascript">
function onSubmit(){
     y=document.changePS.oldpwd.value;
     z=document.changePS.newpwd.value;
     a=document.changePS.confpwd.value;
     passR=/^(?=.*\d)(?=.*[A-Z]).{6,20}$/;
    if(z != a){
        alert("New Password and Confirm Password do not match");
        document.getElementById("confpwd").focus();
        return false;
    }
    if(!y.match(passR)){
        alert("Old Password must contain at least one number,one UpperCase letters");
        document.getElementById("oldpwd").focus();
        return false;
    }
    if(!z.match(passR)){
        alert("New Password must contain at least one number,one UpperCase letters");
        document.getElementById("newpwd").focus();
        return false;
    }
    return true;
}
</body>
</html>

更改密码.java

package com.wipro.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;

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 javax.servlet.http.HttpSession;

import com.wipro.util.DButil;

/**
 * Servlet implementation class ChangePassword
 */
@WebServlet("/ChangePassword")
public class ChangePassword extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public ChangePassword() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        try{
            Connection con=DButil.getConnection();
            PrintWriter out=response.getWriter();
            String username = (String)request.getAttribute("username");
            String password=request.getParameter("newPwd");
            String sql="update user_table set password=? where username=?";
            PreparedStatement st=con.prepareStatement(sql);
            st.setString(1, username);
            st.setString(2, password);
            int rs=st.executeUpdate();
                        if(rs!=0) {
                out.println("<h1><font color=green> Updated the password </font></h1>");
            }
            else {
                out.println("<h1><font color=red> Sorry no username with this name </font></h1>"+username);
            }
            }
            catch(Exception e) {
                e.printStackTrace();
            }
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题