我遇到了doGet和doPost方法的问题

时间:2016-01-22 04:38:49

标签: java servlets redirect

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

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

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

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

    我得到 HTTP Status 405 - HTTP method GET is not supported by this URL

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

    我尝试将PersonalInfoOutput.java更改为doGet instead of doPost,但随后Login.java servlet将返回 HTTP Status 405 - HTTP method POST is not supported by this 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>
    
  • 2 个答案:

    答案 0 :(得分:1)

    在ChangePassAdmin.html中,帖子周围的双引号丢失。 您的表单使用时,您的Servlet都应该使用doPost方法&#34; post&#34;行动。

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

    此外,在这种情况下您需要两种方法。在servlet中添加doGet和doPost。表单和doGet将通过链接使用doPost。默认情况下,所有链接都是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()方法。

    答案 1 :(得分:0)

    我认为问题在于您以两种方式使用PersonalInfoOutput servlet:

    1. 从登录servlet转发,通过转发继续使用POST方法
    2. 来自HTML的href,默认情况下使用方法GET
    3. 对于您来说,重构代码不是一个可能的解决方案,这对您来说很有帮助,因此您可以在doGet()中同时实现doPost()PersonalInfoOutput servlet没有重复很多代码?

      或者您可以将员工数据库检索重构为外部类,该外部类将从Login调用而无需从一个servlet重定向到另一个servlet,并在PersonalInfoOutput中实现GET而不是POST。