欢迎访问 生活随笔!

凯发k8官方网

当前位置: 凯发k8官方网 > 前端技术 > javascript >内容正文

javascript

java ee web工程师培训-凯发k8官方网

发布时间:2025/1/21 javascript 26 豆豆
凯发k8官方网 收集整理的这篇文章主要介绍了 java ee web工程师培训-jdbc servlet jsp整合开发之14.servlet请求头信息 小编觉得挺不错的,现在分享给大家,帮大家做个参考.
–典型的请求头信息
–读取http请求头
–使用表格显示所有请求头信息
–理解各种请求头的含义
–区分不同的浏览器类型
##############michael分割线################### • 典型的请求头信息 • 读取http请求头
–使用httpservletrequest中的方法
• 一般方法
–getheader (header名称不区分大小写)
–getheaders
–getheadernames

• 专门方法
–getcookies
–getauthtype
–getremoteuser
–getcontentlength
–getcontenttype
–getdateheader
–getintheader

• 相关信息
–getmethod
–getrequesturi
–getquerystring
–getprotocol
• 使用表格显示有请求头信息 login.html <!doctype html public "-//w3c//dtd html 4.01 transitional//en">    
<html>    
    <head>    
        <title>login.htmltitle>    
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">    
        <meta http-equiv="description" content="this is my page">    
        <meta http-equiv="content-type" content="text/html; charset=utf-8">    
        <!--rel="stylesheet" type="text/css" href="./styles.css">--
>    

    head>    
    <body>    
        <form name="f1" id="f1" action="/servlet_requestheader/servlet/requestheaderservlet" method="post">    
            <table border="0">    
                <tr>    
                    <td>login:td>    
                    <td><input type="text" name="login" id="login">td>    
                tr>    
                <tr>    
                    <td>password:td>    
                    <td><input type="password" name="password" id="password">td>    
                tr>    
                <tr>    
                    <td colspan="2" align="center"><input type="submit" value="login">td>    
                tr>    
            table>    
        form>    
    body>    
html>
requestheaderservlet.java package com.michael.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 requestheaderservlet extends httpservlet {    

        /**    
         * constructor of the object.    
         */
    
        public requestheaderservlet() {    
                super();    
        }    

        /**    
         * destruction of the servlet.
    
         */
    
        public void destroy() {    
                super.destroy(); // just puts "destroy" string in log    
                // put your code here    
        }    

        /**    
         * the doget method of the servlet.
    
         *    
         * 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    
         */
    
        public void doget(httpservletrequest request, httpservletresponse response)    
                        throws servletexception, ioexception {    

                dopost(request,response);    
        }    

        /**    
         * the dopost method of the servlet.
    
         *    
         * 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 {    
                enumeration names = request.getheadernames();                

                response.setcontenttype("text/html");    
                printwriter out = response.getwriter();    
                out    
                                .println("//w3c//dtd html 4.01 transitional//en\">");    
                out.println("");    
                out.println("    a servlet");    
                out.println("    ");    

                out.println("");    
                out.println("");    
                out.println("");    
                out.println("");    
                out.println("");    
                while(names.hasmoreelements()){    
                        string name = (string) names.nextelement();    
                        string value = request.getheader(name);    
                        out.println("");    
                        out.println("");    
                        out.println("");    
                        out.println("");    
                }    
                out.println("
");    
                out.println("requestheader name");    
                out.println("
");    
                out.println("requestheader value");    
                out.println("
");    
                        out.println(name);    
                        out.println("
");    
                        out.println(value);    
                        out.println("
");    
                out.println("    ");    
                out.println("");    
                out.flush();    
                out.close();    
        }    

        /**    
         * initialization of the servlet.
    
         *    
         * @throws servletexception if an error occure    
         */
    
        public void init() throws servletexception {    
                // put your code here    
        }    

}
看下效果 • 理解各种请求头的含义
– accept
• 标识浏览器能处理mime类型
• 能发送不同的内容到不同的客户端. 例如,png文件有好的压缩特性,但是在浏览器中支持的不是很广泛。
• 一个servlet可以检查是否支持png文件格式,如果支持
– 否则

– accept-encoding
• 标识浏览器能处理的编码类型
– authorization
• 授权信息,通常出现在对服务器发送的www-authenticate头的应答中
– connection
• 表示是否需要持久连接。如果servlet看到这里的值为“keep-alive”,或者看到请求使用的是http 1.1(http 1.1默认进行持久连接),它就可以利用持久连接的优点,当页面包含多个元素时(例如applet,图片),显著地减少下载所需要的时间。要实现这一点,servlet需要在应答中发送一个content-length头,最简单的实现方法是:先把内容写入bytearrayoutputstream,然后在正式写出内容之前计算它的大小。
– cookie
• 参考java ee web工程师培训-jdbc servlet jsp整合开发之16.cookie –host
• 包含一个url,用户从该url代表的页面出发访问当前请求的页面
–if-modified-since
• 只有当所请求的内容,在指定的日期之后,又经过修改才返回它,否则返回304“not modified”应答
–referer
• 包含一个url,用户从该url代表的页面出发访问当前请求的页面。
–user-agent
• 浏览器类型,如果servlet返回的内容与浏览器类型有关则该值非常有用。 • 区分不同的浏览器类型 browsertypeservlet.java package com.michael.servlet;    

import java.io.ioexception;    
import java.io.printwriter;    

import javax.servlet.servletexception;    
import javax.servlet.http.httpservlet;    
import javax.servlet.http.httpservletrequest;    
import javax.servlet.http.httpservletresponse;    

public class browsertypeservlet extends httpservlet {    

        /**    
         * constructor of the object.    
         */
    
        public browsertypeservlet() {    
                super();    
        }    

        /**    
         * destruction of the servlet.
    
         */
    
        public void destroy() {    
                super.destroy(); // just puts "destroy" string in log    
                // put your code here    
        }    

        /**    
         * the doget method of the servlet.
    
         *    
         * 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    
         */
    
        public void doget(httpservletrequest request, httpservletresponse response)    
                        throws servletexception, ioexception {    

                dopost(request,response);    
        }    

        /**    
         * the dopost method of the servlet.
    
         *    
         * 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 {    
                string browsername = request.getheader("user-agent");    
                string result="";    
                if(browsername.indexof("msie")!=-1){    
                        result = "您当前使用的浏览器是ie!";    
                }else{    
                        result = "您当前使用的浏览器是firefox!";    
                }    

                response.setcontenttype("text/html;charset=gbk");    
                printwriter out = response.getwriter();    
                out    
                                .println("//w3c//dtd html 4.01 transitional//en\">");    
                out.println("");    
                out.println("    a servlet");    
                out.println("    ");    
                out.println(result);    
                out.println("    ");    
                out.println("");    
                out.flush();    
                out.close();    
        }    

        /**    
         * initialization of the servlet.
    
         *    
         * @throws servletexception if an error occure    
         */
    
        public void init() throws servletexception {    
                // put your code here    
        }    

}
测试 ok! ##############michael分割线###################

总结

以上是凯发k8官方网为你收集整理的java ee web工程师培训-jdbc servlet jsp整合开发之14.servlet请求头信息的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得凯发k8官方网网站内容还不错,欢迎将凯发k8官方网推荐给好友。

  • 上一篇:
  • 下一篇:
网站地图