servlet中form表单中action的问题
jsp文件-------<%@pagelanguage="java"import="java.util.*"pageEncoding="gbk"%><!DOCTYPEHT...
jsp文件-------
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>注册页面</title>
</head>
<body>
<form action="servlet/RegServlet" method="post">
<center>
<table cellpadding="2" bgcolor="">
<tr>
<td><strong style="color: red;">*</strong>用户名</td>
<td><input type="text" name="username" size="30"></td>
</tr>
<tr>
<td><strong style="color: red;">*</strong>密码</td>
<td><input type="password" name="passwd" size="30"></td>
</tr>
<tr>
<td><strong style="color: red;">*</strong>密码确认</td>
<td><input type="password" name="passwd_confirmation" size="30"></td>
</tr>
<tr>
<td><input type="submit" value="同意条款并注册"/></td>
</tr>
</table>
</center>
</form>
</body>
</html>
web.xml文件------
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>RegServlet</servlet-name>
<servlet-class>com.servlet.RegServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RegServlet</servlet-name>
<url-pattern>/servlet/RegServlet</url-pattern>
</servlet-mapping>
</web-app>
java文件---------
public class RegServlet extends HttpServlet {
public RegServlet(){
super();
}
public void destroy(){
super.destroy();
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
doPost(request,response);
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
String username=new String(request.getParameter("username").getBytes("iso_8859_1"),"gb2312");
String passwd = request.getParameter("passwd");
String passwd_confirmation=request.getParameter("passwd_confirmation");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
public void init()throws ServletException{
}
}
点击提交之后总是弹出404错误..望各位哥哥姐姐帮帮妹妹..刚刚开始学..实在找不出错误.. 展开
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>注册页面</title>
</head>
<body>
<form action="servlet/RegServlet" method="post">
<center>
<table cellpadding="2" bgcolor="">
<tr>
<td><strong style="color: red;">*</strong>用户名</td>
<td><input type="text" name="username" size="30"></td>
</tr>
<tr>
<td><strong style="color: red;">*</strong>密码</td>
<td><input type="password" name="passwd" size="30"></td>
</tr>
<tr>
<td><strong style="color: red;">*</strong>密码确认</td>
<td><input type="password" name="passwd_confirmation" size="30"></td>
</tr>
<tr>
<td><input type="submit" value="同意条款并注册"/></td>
</tr>
</table>
</center>
</form>
</body>
</html>
web.xml文件------
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>RegServlet</servlet-name>
<servlet-class>com.servlet.RegServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RegServlet</servlet-name>
<url-pattern>/servlet/RegServlet</url-pattern>
</servlet-mapping>
</web-app>
java文件---------
public class RegServlet extends HttpServlet {
public RegServlet(){
super();
}
public void destroy(){
super.destroy();
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
doPost(request,response);
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
String username=new String(request.getParameter("username").getBytes("iso_8859_1"),"gb2312");
String passwd = request.getParameter("passwd");
String passwd_confirmation=request.getParameter("passwd_confirmation");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
public void init()throws ServletException{
}
}
点击提交之后总是弹出404错误..望各位哥哥姐姐帮帮妹妹..刚刚开始学..实在找不出错误.. 展开
3个回答
展开全部
首先你要确保你的xml文件Servlet的映射没写错
- <servlet>
<servlet-name></servlet-name>
<servlet-class></servlet-class>
</servlet>
- <servlet-mapping>
<servlet-name></servlet-name>
<url-pattern>/servlet/RegServlet</url-pattern>
注意url-pattern里最前边的 / 一定要加
还有你的html文件 一定要放在项目的根目录下 你能放在自己建的文件夹里
- <servlet>
<servlet-name></servlet-name>
<servlet-class></servlet-class>
</servlet>
- <servlet-mapping>
<servlet-name></servlet-name>
<url-pattern>/servlet/RegServlet</url-pattern>
注意url-pattern里最前边的 / 一定要加
还有你的html文件 一定要放在项目的根目录下 你能放在自己建的文件夹里
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
form写的有问题
<form action="<%request.getContextPath()%>/servlet/RegServlet" method="post">
<form action="<%request.getContextPath()%>/servlet/RegServlet" method="post">
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
404是路径错误。你在servlet/RegServlet 前面加上项目名试试。
更多追问追答
追问
试过了,还是不行...
追答
把出现404错误的时候浏览器的地址贴出来
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询