该工程的名称是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. [转]SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)

    原文地址:http://blog.csdn.net/zhshulin/article/details/37956105#comments 使用SSM(Spring.SpringMVC和Mybatis) ...

  2. Image Segmentation的定义

    Definition 图像分割将一张图分为\(n\)个region, 需要满足下面5个条件 每一个像素都要属于一个region 每个region都是连通的 region与region之间没有交集 re ...

  3. 100722B

    在stack里套set,然后每次根据他的操作,在set里操作,把括号hash,插入,输出set的size-1 #include<iostream> #include<set> ...

  4. 数据库开发基础-SQl Server 链接查询

    连接查询:通过连接运算符可以实现多个表查询.连接是关系数据库模型的主要特点,也是它区别于其它类型数据库管理系统的一个标志. 常用的两个链接运算符: 1.join   on 2.union     在关 ...

  5. Visual Studio Code 添加设置代码段(snippet)

    从VSCode发布以来就在关注,最近已经更新到版本0.10.8,已经支持了插件功能.日常使用编辑器已经由Sublime Text迁移到了VSCode.使用中遇到了这个问题,在网上也没搜到解决方案.记录 ...

  6. 成为JavaGC专家(1)—深入浅出Java垃圾回收机制

    转载自:http://www.importnew.com/1993.html 对于Java开发人员来说,了解垃圾回收机制(GC)有哪些好处呢?首先可以满足作为一名软件工程师的求知欲,其次,深入了解GC ...

  7. BZOJ 1142: [POI2009]Tab

    1142: [POI2009]Tab Time Limit: 40 Sec  Memory Limit: 162 MBSubmit: 213  Solved: 80[Submit][Status][D ...

  8. [学习笔记]四边形不等式优化DP

    形如$f[i][j]=min{f[i][k]+f[k+1][j]}+w[i][j]$的方程中, $w[\;][\;]$如果同时满足: ①四边形不等式:$w[a][c]+w[b][d]\;\leq\;w ...

  9. Linux下的删除命令

    Linux:rm Windows:del rm parameter: -f, --force    忽略不存在的文件,从不给出提示.-i, --interactive 进行交互式删除-r, -R, - ...

  10. NAnt0.92版本首次在windows 8.1的机子上运行报错的问题解决

    在官网上下载的0.92版本,各方面都配置好之后,用命令行运行,却提示报错,如下: 具体的错误提示文字是这样的: 获取ConfigurationFileLocation异常. System.Securi ...