一个Servlet基础题,大家来帮我看看
编写一个Servlet程序,在方法中显示一个表单,用户可以输入姓名和电子邮件地址,用户提交该表单后,在方法中动态地读出请求参数,并输出这些参数.
package thirdservlet;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
/**
* <p> Title: </p>
*
* <p> Description: </p>
*
* <p> Copyright: Copyright (c) 2007 </p>
*
* <p> Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class ThirdServlet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK ";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
String username = request.getParameter( "username ");
String useremail = request.getParameter( "useremail ");
if(username == null||useremail == null) {
out.println( " <html> ");
out.println( " <head> <title> ThirdServlet </title> </head> ");
out.println( " <body bgcolor=\ "#ffffff\ "> ");
out.println(
" <form method=\ "post\ " action=\ "/ServletModuel/ThirdServlet\ "> ");
out.println( " <table border=\ "0\ " width=\ "80%\ " align=\ "center\ "> ");
out.println( " <tr> <td height=\ "80\ "> 姓名 </td> ");
out.println( " <td> <input type=\ "text\ " name=\ "username\ "> </td> </tr> ");
out.println( " <tr> ");
out.println( " <td height=\ "80\ "> 电子邮件 </td> ");
out.println(
" <td> <input type=\ "text\ " name=\ "useremail\ "> </td> </tr> ");
out.println( " <tr> <td height=\ "80\ "> </td> ");
out.println( " <td> <input type=\ "submit\ " name=\ "Submit\ "> ");
out.println( " <input type=\ "reset\ " name=\ "Reset\ "> </td> </tr> ");
out.println( " </table> ");
out.println( " </form> ");
out.println( " </body> ");
out.println( " </html> ");
} else {
userInterface(request,response,username,useremail);
}
out.close();
}
/**
* userInterface
*
* @param response HttpServletResponse
*/
private void userInterface(HttpServletRequest request,HttpServletResponse
response,String username,String useremail)
throws IOException {
PrintWriter out = response.getWriter();
Enumeration e = request.getParameterNames();
out.println( " <html> ");
out.println( " <head> <title> ThirdServlet </title> </head> ");
out.println( " <body bgcolor=\ "#ffffff\ "> ");
out.println( " <table border=\ "0\ " width=\ "80%\ " align=\ "center\ "> ");
if(e.hasMoreElements()) {
out.println( " <tr> <td height=\ "80\ "> "+ e.nextElement().toString()
+ " </td> <td> "+ username + " </td> </tr> ");
}
if(e.hasMoreElements()) {
out.println( " <tr> <td height=\ "80\ "> "+ e.nextElement().toString()
+ " </td> <td> "+ useremail + " </td> </tr> ");
}
out.println( " </body> ");
out.println( " </html> ");
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request,response);
}
//Clean up resources
public void destroy() {
}
}
为什么我把信息输入完后点提交,就报
StandardHost[localhost]: MAPPING configuration error for request URI /ServletModuel/ThirdServlet
[解决办法]
你的web.xml文件?
[解决办法]
你用到struts了 为什么还要自己写servlet
把struts的配置去掉
[解决办法]
改成下面这样试试.
<servlet-mapping>
<servlet-name> thirdservlet </servlet-name>
<url-pattern> /ServletModuel/ThirdServlet </url-pattern>
</servlet-mapping>