第一个是jsp文件。代码如下:
<%@ page language="java" import="java.util.*" contentType="text/html; charset=gb2312"%>
<%
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 'userform2.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>
<form action="dopost" method="post" >;
用户名:<input type=text size="10" name="username" />
密 码:<input type=text size="10" name="password" />
<input type="submit" value="提交">
</form>
</body>
</html>
第二个是java文件
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 dopost extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 8777766643142057158L;
/**
* Constructor of the object.
*/
public dopost() {
super();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
request.setCharacterEncoding("gb2312");
String name=request.getParameter("username");
String word=request.getParameter("password");
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("用户名"+ name +"<br>");
out.print("密 码" + word +"<br>" );
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
但是提交后显示404错误,描述是The requested resource (/prj_test/dopost) is not available.
毕设要求,本人菜鸟一只,入门两个礼拜,现学现卖中,求高人解答。
------解决方案--------------------
在第一个 jsp文件中的form表单中属性action='post',你需要在web.xml文件中配置<servlet>和<servlet-mapping>标签
例子:
<servlet>
<servlet-name>post</servlet-name>