HttpServlet方法如下:
JAVA代码如下:
package shark.zd.servlets; import java.io.IOException; import java.io.PrintWriter; import java.util.Enumeration;
import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.xml.ws.Response;
import org.omg.CORBA.Request;
import com.sun.jmx.snmp.Enumerated;
public class TestHttpServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) { execute(request, response); }
public void doPost(HttpServletRequest request, HttpServletResponse response) { execute(request, response); } public void execute(HttpServletRequest request, HttpServletResponse response){ //容器:装载信息object
/* * Enumerationg有几个方法 * 1,判断内部有没有任务.hasMoreElements * 2,获得下一个信息.nextElement */ Enumeration headers = request.getHeaderNames(); PrintWriter pw; try { pw = response.getWriter(); pw.print(""); pw.print(""); pw.print(""); pw.print("头信息名"); pw.print(" | "); pw.print(""); pw.print("头信息值"); pw.print(" | "); pw.print(" "); while(headers.hasMoreElements()){ Object obj = headers.nextElement(); String headName = String.valueOf(obj); String headValue = request.getHeader(headName); pw.print(""); pw.print(""); pw.print(headName); pw.print(" | "); pw.print(""); pw.print(headValue); pw.print("/td"); pw.print(" | "); } pw.print(" "); pw.close(); } catch (IOException e) { // TODO Auto-generated catch block
e.printStackTrace(); } } }
|
HTML代码如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>test.html</title>
</head> <body> <form action="wlecome" method="get"> 请输入你的名字:<input type="text" name="username" /></br> <input type="radio" name="testRadio" value="man">男</br> <input type="radio" name="testRadio" value="woman">女</br> <select name="sw"> <option value="s">s</option> <option value="w">w</option> </select> <input type="checkbox" name="c1" value="c1"> <input type="checkbox" name="c2" value="c2"> <input type="checkbox" name="c3" value="c3"> <input type="checkbox" name="c4" value="c4"> <input type="submit" value="提交" /> </form> <a href="testMethod">超链接方法</a>> </body> </html>
|
web.xml代码如下:
<!-- servlet的名字和本个Servlet需要访问java类文件全地址 --> <servlet> <servlet-name>testMethodServlet</servlet-name> <servlet-class>shark.zd.servlets.TestHttpServlet</servlet-class>
</servlet> <!-- 寻找外部访问url的Servletname --> <servlet-mapping> <servlet-name>testMethodServlet</servlet-name> <!-- 允许外部访问的url --> <url-pattern>/testMethod</url-pattern> </servlet-mapping>
|
实验结果:得到访问网页的各个信息
阅读(5118) | 评论(1) | 转发(0) |