JSP中form表单action使用servlet类的404错误
jsp代码:<%@pagecontentType="text/html;charset=GBK"%><html><head><title>通过get方法上传变量</tit...
jsp代码:
<%@ page contentType="text/html; charset=GBK"%>
<html>
<head>
<title>通过get方法上传变量</title>
</head>
<body>
<center>
<h3>通过get方法上传变量</h3>
<form method="get" action="servlet/servletlife1">
<p>名字:<input type="TextField" name="name" value=""></p>
<p>性别:<input type="TextField" name="gender" value=""></p>
<p>年龄:<input type="TextField" name="age" value=""></p>
<input type="submit" name="Sumit" value="提交">
<input type="reset" value="重写">
</form>
</center>
</body>
</html>
servlet代码:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class servletlife1 extends HttpServlet {
static final private String CONTENT_TYPE="text/html; charset=GBK";
public void init() throws ServletException {
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("CONTENT_TYPE");
request.setCharacterEncoding("GBK");
PrintWriter out = response.getWriter();
String name=request.getParameter("name");
String gender=request.getParameter("gender");
String age=request.getParameter("age");
out.println("<HTML>");
out.println(" <HEAD><TITLE>doget示例</TITLE></HEAD>");
out.println(" <BODY>");
out.println("<center>");
out.println("<p>名字 ="+name+"</p>");
out.println("<p>性别 ="+gender+"</p>");
out.println("<p>名字 ="+age+"</p>");
out.println("</center>");
out.println(" </BODY>");
out.println("</HTML>");
}
public void destroy(){
}
}
web.xml:
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">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>servletlife1</servlet-name>
<servlet-class>be.servletlife1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servletlife1</servlet-name>
<url-pattern>/servlet/servletlife1</url-pattern>
</servlet-mapping>
</web-app>
当单击提交按钮时,出现的错误:
HTTP Status 404 - /dd/my/servlet/servletlife1
--------------------------------------------------------------------------------
type Status report
message /dd/my/servlet/servletlife1
description The requested resource (/dd/my/servlet/servletlife1) is not available.
--------------------------------------------------------------------------------
Apache Tomcat/6.0.29
问题补充:
即使把web.xml中的<url-pattern>/servlet/servletlife1</url-pattern>改成<url-pattern>/servletlife1</url-pattern>也是不行
急!急!急! 展开
<%@ page contentType="text/html; charset=GBK"%>
<html>
<head>
<title>通过get方法上传变量</title>
</head>
<body>
<center>
<h3>通过get方法上传变量</h3>
<form method="get" action="servlet/servletlife1">
<p>名字:<input type="TextField" name="name" value=""></p>
<p>性别:<input type="TextField" name="gender" value=""></p>
<p>年龄:<input type="TextField" name="age" value=""></p>
<input type="submit" name="Sumit" value="提交">
<input type="reset" value="重写">
</form>
</center>
</body>
</html>
servlet代码:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class servletlife1 extends HttpServlet {
static final private String CONTENT_TYPE="text/html; charset=GBK";
public void init() throws ServletException {
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("CONTENT_TYPE");
request.setCharacterEncoding("GBK");
PrintWriter out = response.getWriter();
String name=request.getParameter("name");
String gender=request.getParameter("gender");
String age=request.getParameter("age");
out.println("<HTML>");
out.println(" <HEAD><TITLE>doget示例</TITLE></HEAD>");
out.println(" <BODY>");
out.println("<center>");
out.println("<p>名字 ="+name+"</p>");
out.println("<p>性别 ="+gender+"</p>");
out.println("<p>名字 ="+age+"</p>");
out.println("</center>");
out.println(" </BODY>");
out.println("</HTML>");
}
public void destroy(){
}
}
web.xml:
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">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>servletlife1</servlet-name>
<servlet-class>be.servletlife1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servletlife1</servlet-name>
<url-pattern>/servlet/servletlife1</url-pattern>
</servlet-mapping>
</web-app>
当单击提交按钮时,出现的错误:
HTTP Status 404 - /dd/my/servlet/servletlife1
--------------------------------------------------------------------------------
type Status report
message /dd/my/servlet/servletlife1
description The requested resource (/dd/my/servlet/servletlife1) is not available.
--------------------------------------------------------------------------------
Apache Tomcat/6.0.29
问题补充:
即使把web.xml中的<url-pattern>/servlet/servletlife1</url-pattern>改成<url-pattern>/servletlife1</url-pattern>也是不行
急!急!急! 展开
展开全部
action="servlet/servletlife1"加个斜杠改成action="/servlet/servletlife1"试一试
还有web.xml里怎么多了个<servlet>
哦,那个斜杠不要加了,jsp改成这样就应该没有问题了
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
This is my JSP page. <br>
<form method="GET" action="servlet/test">
<input type="submit" value="submit"/>
</form>
</body>
</html>
就是注意<@page>下边那一段,还有<head>里的<base>标签,其它的倒是无所谓
反正就是那些相对路径的问题
还有web.xml里怎么多了个<servlet>
哦,那个斜杠不要加了,jsp改成这样就应该没有问题了
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
This is my JSP page. <br>
<form method="GET" action="servlet/test">
<input type="submit" value="submit"/>
</form>
</body>
</html>
就是注意<@page>下边那一段,还有<head>里的<base>标签,其它的倒是无所谓
反正就是那些相对路径的问题
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |