该工程的名称是testhttp,功能是在页面中表格打印浏览过程中的相关头信息。

  新建一个工程,然后在这个工程里面新建一个servlet,这样便可以省去编写web.xml的过程

以下是TestHttpServlet.java中的代码

package org.common.servlet;

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; public class TestHttpServlet extends HttpServlet { /**
* Constructor of the object.
*/
public TestHttpServlet() {
super();
} /**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
} /**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/ //Servlet接口的参数service(ServletRequest arg0, ServletResponse arg1) //HttpServletRequest封装了所有的请求信息,其实就是Tomcat将请求信息按照JAVA EE的Servlet的规范
//封装好给我们 public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { //设置返回类型
//response.setContentType("text/html;charset=GBK");
response.setContentType("text/html");
response.setCharacterEncoding("GBK"); //获取输出流
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>testhttp Servlet</TITLE></HEAD>");
out.println(" <BODY>"); out.print("<center>");
out.print(" <h2>请求头信息列表<h2> "); out.print(" <table border=1> ");
out.print(" <tr><th>名字</th><th>值</th></tr> ");
//返回头消息名字集合,返回的是一个枚举
Enumeration enums = request.getHeaderNames();
//遍历获取所有头信息名和值
while(enums.hasMoreElements()){
//获取每一个头消息的名字
String headName = (String)enums.nextElement();
out.println(" <tr> ");
out.println(" <td> " + headName + " </td> ");
//getHeader(java.lang.String name)
//Returns the value of the specified request header as a String.
//返回这个名字的头信息的值
out.println(" <td> " + request.getHeaders(headName) + " </td> ");
out.println(" </tr> ");
}
out.println(" </table> "); out.println(" <hr> ");
//测试HttpServletRequest的方法
out.println("Method: " + request.getMethod() + "<br>");
out.println("Request URI: " + request.getRequestURI() + "<br>");
out.println("Protocol: " + request.getProtocol() + "<br>");
out.println("PathInfo: " + request.getPathInfo() + "<br>");
out.println("Remote Address: " + request.getRemoteAddr() + "<br>");
out.println("ContextPath: " + request.getContextPath() + "<br>");
out.println("getScheme: " + request.getScheme() + "<br>");
out.println("getServerName: " + request.getServerName() + "<br>");
out.println("getServerPort: " + request.getServerPort() + "<br>");
out.println("getRequestURI: " + request.getRequestURI() + "<br>");
String path = request.getContextPath();
//请求全路径
String basePath
= request.getScheme() + "://" + request.getServerName() + ":"
+ request.getServerPort() + request.getRequestURI();
out.println(" path: " + path + "<br>");
out.println(" basePath: " + basePath + "<br>"); //out.print(this.getClass());
//out.println(", using the GET method"); out.print("</center>");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
} /**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { //调用doGet方法
this.doGet(request, response);
} /**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
} }

然后部署并启动Tomcat服务器,在浏览器中输入http://127.0.0.1:8080/testhttp/servlet/TestHttpServlet

JavaWeb学习笔记——开发动态WEB资源(四)打印当前使用的是get方法的更多相关文章

  1. JavaWeb学习笔记——开发动态WEB资源(一)Java程序向浏览器输出数据

    开发一个动态web资源,即开发一个Java程序向浏览器输出数据,需要完成以下2个步骤: 1.编写一个Java类,实现Servlet接口 开发一个动态web资源必须实现javax.servlet.Ser ...

  2. JavaWeb学习笔记——开发动态WEB资源(六)ServletConfig和ServletContext

    1.只有在第一次请求服务器产生实例的时候才会调用init()方法,有一种办法能在服务器一启动的时候就加载init()方法. 即服务器启动即加载Servlet,且按数字大小顺序实例化Servlet. 方 ...

  3. JavaWeb学习笔记——开发动态WEB资源(五)servlet身份验证

    本工程的功能是实现Javaweb的servlet身份验证 一下是login.html文件中的代码 <!DOCTYPE html> <html> <head> < ...

  4. JavaWeb学习笔记——开发动态WEB资源(二)HelloWord

    该工程的功能是在页面上输出一段话 首先在src里面新建一个class,在interface里面添加javax.servlet.Servlet 以下是HelloServlet.java中的代码: pac ...

  5. JavaWeb学习笔记——开发动态WEB资源(八)cookies和httpsession

    会话: cookies: (1)cookies是WEB服务器发送到浏览器的简短文本信息 (2)cookies可以禁用 httpsession: 一次会话是从你打开浏览器开始到你关闭浏览器结束 提供一种 ...

  6. JavaWeb学习笔记——开发动态WEB资源(七)bookapp

    该工程的功能是实现一个bookapp 1.开发注册页面,注册使用properties文件,存储在classpath跟路径 2.注册成功跳转到登录页面 3.输入用户名密码登录,登录成功跳转到book显示 ...

  7. JavaWeb学习笔记——开发动态WEB资源(三)显示当前时间

    该工程的功能是实现在页面中显示当前的时间 以下的代码是HelloServlet.java中的代码 package helloapp2; import java.io.IOException; impo ...

  8. Ruby学习笔记4: 动态web app的建立

    Ruby学习笔记4: 动态web app的建立 We will first build the Categories page. This page contains topics like Art, ...

  9. Ruby学习笔记6: 动态web app的建立(3)--多Model之间的交互

    We first built a static site which displayed a static image using only a Controller and a View. This ...

随机推荐

  1. springMVC+mybatis 增删该操作后判断影响行数一直返回-2147482646

    MyBatis发现更新和插入返回值一直为"-2147482646"的错误是由defaultExecutorType设置引起的,如果设置为BATCH,更新返回值就会丢失.mybati ...

  2. jQuery报 SyntaxError: expected expression, got '<'错误

    这有什么可奇怪的,这个问题是表达式未能按照预期结束,说白了就是你少写分号了. 你肯定是语法错了,仔细查看一下提示错误的那一行和它的附近,是不是因为疏忽大意出错了. 再给你的建议,不要觉得某个分号可以省 ...

  3. 【CodeVS 1198】【NOIP 2012】国王游戏

    http://codevs.cn/problem/1198/ 推导一翻,排好序后,直接上高精度. #include<cstdio> #include<cstring> #inc ...

  4. caffe使用

    训练时, solver.prototxt中使用的是train_val.prototxt ./build/tools/caffe/train -solver ./models/bvlc_referenc ...

  5. phpwind9.0模板制作教程——制作论坛风格

    由于论坛模板机制和门户等模板机制不同,所以今天我就先重点讲讲论坛模板制作的大概过程. 一.先来熟悉下phpwind9.0的论坛模板机制. 其实phpwind9.0的模板机制和discuzx2.5差不多 ...

  6. Mac OS 下 eclipse中文乱码解决方法(eclipse for mac 中文乱码)

    由于一些java源码是从其他人那里拷贝过来,放入Mac os 版本的eclipse下,发现中文都是乱码.经过小试,可以解决. 1.打开eclipse 偏好设置 2.General ——>Cont ...

  7. glyphicon halflings regular ttf 报错

    一个web项目 用了bootstrap chrome开f12报错提示glyphicon halflings regular ttf找不到 为什么找不到,肯定又是path出了问题 找到bootstrap ...

  8. 【BZOJ-4521】手机号码 数位DP

    4521: [Cqoi2016]手机号码 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 303  Solved: 194[Submit][Status ...

  9. 【poj2741】 Colored Cubes

    http://poj.org/problem?id=2741 (题目链接) 题意 给出n个骰子,每一面都有一种颜色,问最少更改多少个面的颜色可以使所有骰子通过旋转后完全相同. solution 迷之d ...

  10. [IOS多线程]的使用:防止进行HTTP数据请求时,UI卡死。

    多线程的实现:NSThread 1.子线程的创建:两种方法 第一种: [NSThread detachNewThreadSelector:@selector(downloadImage:) toTar ...