jsp - 无法从 jsp 调用 servlet 文件 - 发现 404 错误
问题描述
我的索引页面已更改为 Register_1.jsp 并在 xml 文件中进行了更新,页面运行良好,操作字段连接 servlet 文件 Guru_register 我在下图中的 eclipse 中添加了 jsp 代码以及 java 代码和结构,请帮助我谢谢提前...!!
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);
}
}
}
我以两种方式尝试过提交带有值的表单并且没有任何一种方式得到相同的错误。
解决方案
你需要在action中给Servlet Name,不需要指定任何东西;
<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;
推荐阅读
- ios - 如何更新 UITableViewCell?
- python - OperationalError:250003:未能获得响应。绞刑?方式:邮寄
- python - Keras LSTM 用于将句子转换为文档上下文向量
- swift - Swift - 无法同时满足约束
- git - 为什么 git push 失败了?错误:未能将一些参考推送到
- javascript - 我需要使用带有奇怪 Google 电子表格链接的 Javascript 创建一个 HTML 表。我该怎么做呢?
- android - 为 Android 创建一个手电筒/手电筒应用程序
- java - 从 Java 运行节点命令行模块
- android - 无法使闪光灯连续闪烁并在单击按钮时停止
- tensorflow - 将 C++ TensorFlow 与 WebAssembly 后端结合使用