JavaWeb学习笔记——开发动态WEB资源(四)打印当前使用的是get方法
该工程的名称是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方法的更多相关文章
- JavaWeb学习笔记——开发动态WEB资源(一)Java程序向浏览器输出数据
开发一个动态web资源,即开发一个Java程序向浏览器输出数据,需要完成以下2个步骤: 1.编写一个Java类,实现Servlet接口 开发一个动态web资源必须实现javax.servlet.Ser ...
- JavaWeb学习笔记——开发动态WEB资源(六)ServletConfig和ServletContext
1.只有在第一次请求服务器产生实例的时候才会调用init()方法,有一种办法能在服务器一启动的时候就加载init()方法. 即服务器启动即加载Servlet,且按数字大小顺序实例化Servlet. 方 ...
- JavaWeb学习笔记——开发动态WEB资源(五)servlet身份验证
本工程的功能是实现Javaweb的servlet身份验证 一下是login.html文件中的代码 <!DOCTYPE html> <html> <head> < ...
- JavaWeb学习笔记——开发动态WEB资源(二)HelloWord
该工程的功能是在页面上输出一段话 首先在src里面新建一个class,在interface里面添加javax.servlet.Servlet 以下是HelloServlet.java中的代码: pac ...
- JavaWeb学习笔记——开发动态WEB资源(八)cookies和httpsession
会话: cookies: (1)cookies是WEB服务器发送到浏览器的简短文本信息 (2)cookies可以禁用 httpsession: 一次会话是从你打开浏览器开始到你关闭浏览器结束 提供一种 ...
- JavaWeb学习笔记——开发动态WEB资源(七)bookapp
该工程的功能是实现一个bookapp 1.开发注册页面,注册使用properties文件,存储在classpath跟路径 2.注册成功跳转到登录页面 3.输入用户名密码登录,登录成功跳转到book显示 ...
- JavaWeb学习笔记——开发动态WEB资源(三)显示当前时间
该工程的功能是实现在页面中显示当前的时间 以下的代码是HelloServlet.java中的代码 package helloapp2; import java.io.IOException; impo ...
- Ruby学习笔记4: 动态web app的建立
Ruby学习笔记4: 动态web app的建立 We will first build the Categories page. This page contains topics like Art, ...
- 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 ...
随机推荐
- Mybatis学习--Mapper.xml映射文件
简介 Mapper.xml映射文件中定义了操作数据库的sql,每个sql是一个statement,映射文件是mybatis的核心. 映射文件中有很多属性,常用的就是parameterType(输入类型 ...
- ubuntu静态IP配置
1. 修改配置文件/etc/network/interfacesroot@ubuntu:~# sudo gedit /etc/network/interfaces 添加以下内容:auto eth0 ...
- [转]response.getWriter().write()与out.print()的区别
原文地址:http://blog.csdn.net/javaloveiphone/article/details/8133772 1.首先介绍write()和print()方法的区别: (1).w ...
- 【BZOJ 2693】jzptab
莫名其妙地又卡在long long上了,我果然又在同一个地方犯逗. 在“在路上,同梦行”群里闹了个大笑话QAQ #include<cstdio> #include<cstring&g ...
- 【POJ 3261】Milk Patterns 可重叠的k次最长重复子串
可重叠的k次最长重复子串 #include<cstdio> #include<cstring> #include<algorithm> using namespac ...
- Ext-设置form表单不可编辑
1. var formEach = win.down('form').items.items; Ext.each(formEach,function(item){ console.log(item); ...
- python3.x + selenium 3.x 遇到的问题:Exception ignored in: <bound method Service.__del__ of <selenium.webdriver.firefox.service.Service object at 0x0045E450>>
我解决了!!!from selenium import webdriverimport timedr = webdriver.Firefox(executable_path = '/Users/jin ...
- yii自动登录
在yii,登录页面选择记住密码,下次就会自动登陆 前些天,自己增加了一个web应用,但是发现虽然选择记住密码,没选退出,关闭浏览器,重新进入还会跳转到登陆页面 自动登录是利用cookie实现的 配置U ...
- finally关键字
final:禁止多态开关~修饰变量:变量不能被改变修饰类:类不能被继承修饰方法:方法不能被重写 finally:用在异常处理的最后一个语句块无论是否产生异常都要被执行~~~ Java代码 public ...
- pyhthon --递归,装饰器
递归: 递归的概念很简单,如果函数包含了对其自身的调用,该函数就是递归.拗口一点的定义是,如果一个新的调用能在相同过程中较早的调用结束之前开始,那么该过程就是递归. """ ...