gpt4 book ai didi

java - 我在使用 doGet 和 doPost 方法时遇到问题

转载 作者:行者123 更新时间:2023-11-30 07:39:10 25 4
gpt4 key购买 nike

PersonalInfoOutput.java servlet 正在使用 doPost 方法。用户登录index.html从index.html,请求被发送到Login.java servlet。

从那里,用户被定向到 PersonalInfoOutput.java servlet。

现在,考虑另一个名为 ChangePassAdmin.html 的页面。在此 html 代码中,其中一个

  • 转到 PersonalInfoOutput.java。但是当我从该页面单击它时,

    我收到HTTP 状态 405 - 此 URL 不支持 HTTP 方法 GET

    有人可以帮我解决这个问题吗?

    我尝试将 PersonalInfoOutput.java 更改为 doGet 而不是 doPost,但 Login.java servlet 将返回 HTTP 状态 405 - HTTP 方法 POST 是此 URL 不支持。所以看来我需要这个 PersonalInfoOutput.java servlet 的 doGet 和 doPost 。

    PersonalInfoOutput.java(servlet)

    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.sql.*;

    public class PersonalInfoOutput extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();

    HttpSession session = request.getSession(false);
    String employeeid = "";

    if(session != null) {
    employeeid = (String)session.getAttribute("employeeid");
    }


    boolean st = false;
    try {
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/payroll_system", "root", "");
    PreparedStatement ps = con.prepareStatement("select employeeID, FirstName, LastName, Admin, DOB, Address, Email, HourlyRate, Gender, ALeaveBalance, SLeaveBalance, ActiveStatus, Role, BSB, BankName, AccNumber, SuperNumber, SuperCompany from payroll_system.employee_info where employeeID = ?");
    ps.setString(1, employeeid);
    ResultSet rs = ps.executeQuery();
    st = rs.next();
    if(st){
    etc... (rest of the code isn't relevant to question)

    index.html

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <link rel = "stylesheet" type = "text/css" href = "main.css">
    <title>Login</title>
    </head>
    <body>
    <form action="Login" method="post">
    <h1>
    Login
    </h1>
    <b>Employee ID:</b> <br>
    <input type="text"name="employee_id" size="20"><br><br>
    <b>Password:</b><br>
    <input type="password" name="password" size="20"><br><br>
    <input type="submit" value="Login"><br><br>
    </form>
    </body>
    </html>

    Login.java(servlet)

    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.sql.*;

    public class Login extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();

    String employee_id = request.getParameter("employee_id");
    String password = request.getParameter("password");

    HttpSession session = request.getSession();
    session.setAttribute("employeeid", employee_id);

    if(ValidateLogin.user(employee_id, password)) {
    RequestDispatcher rs = request.getRequestDispatcher("PersonalInfoOutput");
    rs.forward(request, response);
    }
    else
    {
    out.print("Employee ID or Password is incorrect. Please try again.");
    RequestDispatcher rs = request.getRequestDispatcher("index.html");
    rs.include(request, response);
    }
    }


    }

    ChangePassAdmin.html

    <!DOCTYPE html> 
    <html>
    <head>
    <meta charset = "UTF-8">
    <link rel = "stylesheet" type = "text/css" href = "main.css">
    <link rel = "stylesheet" type = "text/css" href = "sidebar.css">
    <title>Change Password</title>
    <style>
    table { border-collapse: collapse; width: 50%; } th, td { text-align: left; padding: 8px; } tr:nth-child(even){background-color: #f2f2f2}
    tr:hover {background-color: #e2f4ff;}
    </style>
    </head>

    <body>

    <ul>

    <li><a href=PersonalInfoOutput>View Personal Information</a></li>
    <li><a href=PersonalInfoOutput>View Expense Claims</a></li>
    <li><a href=PersonalInfoOutput>View Payslips</a></li>
    <li><a class=active >Change Password</a></li>
    <li><a href=PersonalInfoOutput>Maintain Employee Information</a></li>
    <li><a href=PersonalInfoOutput>Maintain Tax Information</a></li>
    <li><a href=PersonalInfoOutput>Maintain Payroll Items</a></li>
    <li><a href=PersonalInfoOutput>Maintain Timesheet</a></li>
    <li><a href=PersonalInfoOutput>Maintain Employee Expenses</a></li>
    <li><a href=PersonalInfoOutput>Run Payroll</a></li>
    <li><a href=PersonalInfoOutput>Generate Reports</a></li>

    </ul>

    <div style=margin-left:25%;padding:1px;>
    </div>

    <div id="container">
    <h1>Change Password</h1>
    <form action ="NewPassword" method = post>

    <table border ="1">

    <tr>
    <td>Existing Password:</td>
    <td><input type = "password" name = "oldpassword" size = "20"></td>
    </tr>

    <tr>
    <td>New Password:</td>
    <td><input type = "password" name = "newpassword" size = "20"></td>
    </tr>

    <tr>
    <td>Confirm New Password</td>
    <td><input type = "password" name = "confirmpassword" size = "20"></td>
    </tr>
    </table>
    <br>
    <br>
    <input type = "submit" value = "Update Password">
    </form>.
    </div>
    </body>

    </html>
  • 最佳答案

    在 ChangePassAdmin.html 中,帖子周围缺少双引号。您的两个 Servlet 都应该具有 doPost 方法,因为您的表单正在使用“post”操作。

    <form action ="NewPassword" method = "post">

    此外,在这种情况下您将需要两种方法。在 servlet 中添加 doGet 和 doPost。 doPost 将由表单使用,doGet 将由链接使用。默认情况下,所有链接都是 GET。你可以这样做:

    编辑:

    public class PersonalInfoOutput extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    // Your servlets code should be here
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    processRequest(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    processRequest(request, response);
    }

    doPost() 方法接收基于 form 的请求。 doGet() 方法接收基于 href 的请求。但在这两种情况下都会调用 processRequest() 方法。

    关于java - 我在使用 doGet 和 doPost 方法时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34939121/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com