创建Servlet的步骤
1.XXX extends HttpServlet
2.Override doGet doPost
3.copy to WEB-INF/classes
4.web.xml
1) <servlet><servlet-name><servlet-class></servlet>
2) <servlet-mapping><url-pattern> /
5.restart server
一。在JavaWeb开发中,用户自定义Servlet类,继承用于专门用于web开发的HttpServlet类。一般Service()方法不需要重写(当需要对服务器进行初始化操作时可重写)。一般用来处理客户端请求的方法是doGet()和doPost()。
遇到的问题:
type: Status report
message: HTTP method GET is not supported by this URL
description: The specified HTTP method is not allowed for the requested resource (HTTP
method GET is not supported by this URL).
原因:继承自HttpServlet的Servlet没有重写对于请求和响应的处理方法:doGet或doPost等方法;默认调
用父类的doGet或doPost等方法。当你已经重写上述方法时,如果程序中有super.doPost(req, resp)仍然报上述错误,原因是父类HttpServlet的doGet或doPost等方法覆盖了你重写的doGet或doPost等方法。父类HttpServlet的doGet或doPost等方法的默认实现是返回状态代码为405的HTTP错误。
解决方法:子类重写doGet或doPost等方法,不要调用父类HttpServlet的doGet或doPost等方法,即去掉super.doGet(request, response)和super.doPost(request, response).
二。1.浏览器端html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>a example for form_servlet</title> <meta name="Generator" content="EditPlus"> <meta name="Author" content=""> <meta name="Keywords" content=""> <meta name="Description" content=""> </head> <body> <h2>A Sample Form using get/post</h2> <form name="showparameters" method="get" action="servlet/form_servlet"> Number:<input type="text" name="itemnumber"/><br/> Quantity:<input type="text" name="quantity"/><br/> Each Price:<input type="text" name="price"/><br/><hr> Name:<input type="text" name="name"/><br/> Addr:<br/><textarea name="addr" rows="3" cols="30"></textarea><br/> Card:<br/> <input type="Radio" name="card" value="viss">viss<br/> <input type="Radio" name="card" value="AAA">AAA<br/> <input type="Radio" name="card" value="BBB">BBB<br/> Card Number:<input type="text" name="cardnumber"/><br/> Repeat Card Number:<input type="text" name="cardnumber"/> <input type="submit"/> </form> </body> </html>
2.Servlet
import java.io.IOException; import java.io.PrintWriter; import java.util.Map; import java.util.Map.Entry; import java.util.Iterator; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class form_servlet extends HttpServlet{ private PrintWriter pw; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub //resp.getWriter().println("<a href='http://www.baidu.com'>goaaa</a>"); resp.setContentType("text/html;charset=gb2312"); pw = resp.getWriter();//获得输出流 String title = "Reading all request parameters"; pw.println("<html><head><title>读取所有参数</title></head>" +"<body bgcolor='#fdf5e6'>\n" +"<h1 align='center'>"+title+"</h1>\n" +"<table border='1' align='center'>\n" +"<th>Parameter Name<th>Parameter Value(s)"); //获得请求参数 Map<String, String[]> map = req.getParameterMap(); Iterator<Entry<String, String[]>> entries = map.entrySet().iterator(); while(entries.hasNext()){ Map.Entry entry = (java.util.Map.Entry) entries.next(); String key = (String) entry.getKey(); pw.println("<tr><td>"+key+"</td>"); String[] values = (String[])entry.getValue(); if(values.length==1){ pw.println("<td align='center'>"+values[0]+"</td>\n</tr>"); }else{ pw.println("<td><ul>\n"); for (int i = 0; i < values.length; i++) { String value = values[i]; pw.println("<li>"+value+"</li>"); } pw.println("</ul>\n"); pw.println("</td>\n</tr>\n"); } } pw.println("</table>\n</body></html>\n"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub super.doPost(req, resp); } }
3.web.xml文件
<?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0" metadata-complete="true"> <servlet> <servlet-name>form_servlet</servlet-name> <servlet-class>form_servlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>form_servlet</servlet-name> <url-pattern>/servlet/form_servlet</url-pattern> </servlet-mapping> </web-app>
注意的问题:1.注意action的url为相对路径(表示相对当前文件所在的路径)servlet/form_servlet
<form name="showparameters" method="get" action="servlet/form_servlet">
2.web.xml配置文件的中url-pattern定义可访问的web路径,写法:必须以“/”开头
<url-pattern>/servlet/form_servlet</url-pattern>
3.<servlet-mapping>和<servlet>可以使多对一的关系,这样可以为一个servlet定义多个可访问的web路径。