无法从jsp调用servlet文件-发现404错误

时间:2018-08-25 01:10:21

标签: jsp servlets http-status-code-404

我的索引页面我也已更改为Register_1.jsp并在xml文件中进行了更新,并且该页面在操作字段中可以很好地运行以连接servlet文件Guru_register,我在下图中的Eclipse中添加了jsp代码以及java代码和结构。帮帮我,谢谢... !!

Structure in eclipse

Register_1.jsp

<body>
<h1>Guru Register Form</h1>
<form action="Guru_register.java" method="post">
            <table style="with: 50%">
                <tr>
                    <td>First Name</td>
                    <td><input type="text" name="first_name" /></td>
                </tr>
                <tr>
                    <td>Last Name</td>
                    <td><input type="text" name="last_name" /></td>
                </tr>
                <tr>
                    <td>UserName</td>
                    <td><input type="text" name="username" /></td>
                </tr>
                    <tr>
                    <td>Password</td>
                    <td><input type="password" name="password" /></td>
                </tr>
                <tr>
                    <td>Address</td>
                    <td><input type="text" name="address" /></td>
                </tr>
                <tr>
                    <td>Contact No</td>
                    <td><input type="text" name="contact" /></td>
                </tr></table>
            <input type="submit" value="Submit" /></form>
</body>

Register_2.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Guru Success Page</title>
</head>
<body>
           <a><b>Welcome User!!!!</b></a>
</body>
</html>

我的servlet类

public class Guru_register extends HttpServlet {
    private static final long serialVersionUID = 1L;


     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String first_name = request.getParameter("first_name");
        String last_name = request.getParameter("last_name");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String address = request.getParameter("address");
        String contact = request.getParameter("contact");
        System.out.println(first_name+" "+contact);
        if(first_name.isEmpty() || last_name.isEmpty() || username.isEmpty() || 
                password.isEmpty() || address.isEmpty() || contact.isEmpty())
        {
            RequestDispatcher req = request.getRequestDispatcher("Register_1.jsp");
            req.include(request, response);
        }
        else
        {
            RequestDispatcher req = request.getRequestDispatcher("Register_2.jsp");
            req.forward(request, response);
        }
    }

}

我用两种方式尝试用带值的形式提交表单,而没有一种错误。

2 个答案:

答案 0 :(得分:0)

您需要在操作中为Servlet名称赋值,无需指定任何内容;

<body>
<h1>Guru Register Form</h1>
<form action="Guru_register" method="post">

//带有注释的Servlet

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

答案 1 :(得分:0)

尝试将servlet信息添加到web.xml 首先将您的servlet移到一个程序包中,将其称为“控制器”,因此文件的新位置将为 src> controllers> Guru_register.java

<form action="guru-register" method="post">

接下来,修改jsp格式如下:

WEBSITE_DISABLE_SCM_SEPARATION