zoukankan      html  css  js  c++  java
  • Servlet的请求HttpServletRequest

    一、从容器到HttpServlet

    1、web容器作了什么

      web容器做的事情就是,创建Servlet实例,并完成Servlet的名称注册及URL模式的对应。在请求来到时,web容器会转发给正确的Servlet来处理请求。

      当请求来到http服务器时,而http服务器转交请求给容器时,容器会创建一个代表档次请求的HttpServletRequest对象,并将请求相关信息设置给该对象。同时,容器会创建一个HttpServletResponse对象,作为稍后要对客户端进行相应的java对象。

         

      接着,容器会根据读取的@webServlet标注或者xml的设置,找出处理该请求的servlet,调用它的service()方法,将创建的HttpServletRequest对象、HttpServletResponse对象传入作为参数,service()方法中会根据HTTP请求的方式,调用对应的doXXX()方法。接着再doXXX()方法中,可以使用request、resposne对象,如:getParameter()取得请求参数,使用getWriter()取得输出用的PrintWriter对象,并进行各项响应处理。对PrintWriter做的输出操作,最后由容器转换为HTTP响应,再由HTTP服务器对浏览器响应。之后容器将对象销毁。

        

    2、关于HttpServletRequest

    2.1 处理请求参数与标头

      String username = request.getParameter("name");指定请求参数名称来取得对应的值。如果传来的是"123"这样的字符串值,必须使用Integer.parseInt()这类方法剖析为基本类型。

      String[] values = request.getParameterValues("param");就像param=10&param=20&param=30,此时用getParameterValues()取得一个String数组。

      如果想要知道请求中有多少请求参数,则可以使用getParameterNames()方法,会返回一个Enumeration对象。

        Enumeration<String> e = request.getParameterNames();

        while(e.hasMoreElements()){

          String param = e.nextElement();

          ...

        }

      对于HTTP标头的信息(Header),可以通过以下方法取得:

        1、getHeader():使用方式与getParameter类似。

        2、getHeaders():与getParameterValues类似。

        3、getHeaderNames():与getParameterNames类似。

      以下的实例如何取得并显示浏览器送出的标头信息。

     1 package cc.openhome;
     2 
     3 import java.io.IOException;
     4 import java.io.PrintWriter;
     5 import java.util.Enumeration;
     6 
     7 import javax.servlet.ServletException;
     8 import javax.servlet.annotation.WebServlet;
     9 import javax.servlet.http.HttpServlet;
    10 import javax.servlet.http.HttpServletRequest;
    11 import javax.servlet.http.HttpServletResponse;
    12 
    13 /**
    14  * Servlet implementation class HeaderServlet
    15  */
    16 @WebServlet("/header.view")
    17 public class HeaderServlet extends HttpServlet {
    18     private static final long serialVersionUID = 1L;
    19        
    20     /**
    21      * @see HttpServlet#HttpServlet()
    22      */
    23     public HeaderServlet() {
    24         super();
    25         // TODO Auto-generated constructor stub
    26     }
    27 
    28     /**
    29      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    30      */
    31     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    32         // TODO Auto-generated method stub
    33         PrintWriter out=response.getWriter();
    34         out.println("<html>");
    35         out.println("<head>");
    36         out.println("<title>HeadServlet</title>");
    37         out.println("</head>");
    38         out.println("<body>");
    39         out.println("<h1>HeaderServlet at "+request.getContextPath()+"</h1>");//取得应用程序环境路径
    40         Enumeration<String> names=request.getHeaderNames();//取得所有标头名称
    41         while(names.hasMoreElements()){
    42             String name=names.nextElement();
    43             out.println(name+": "+request.getHeader(name)+"<br>");//输出所有的标头值
    44         }
    45         out.println("</body>");
    46         out.println("</html>");
    47          out.close();
    48     }
    49 
    50     /**
    51      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    52      */
    53     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    54         // TODO Auto-generated method stub
    55     }
    56 
    57 }
    HeaderServlet

      运行结果:

        

    2.2 getReader()、getInputStream()读取Body内容

      getReader()方法,可以取得一个BufferedReader对象,可以读取Body数据。

      例子1:读取请求Body内容

     1 package cc.openhome;
     2 
     3 import java.io.BufferedReader;
     4 import java.io.IOException;
     5 import java.io.PrintWriter;
     6 
     7 import javax.servlet.ServletException;
     8 import javax.servlet.annotation.WebServlet;
     9 import javax.servlet.http.HttpServlet;
    10 import javax.servlet.http.HttpServletRequest;
    11 import javax.servlet.http.HttpServletResponse;
    12 
    13 /**
    14  * Servlet implementation class BodyServlet
    15  */
    16 @WebServlet("/body.view")
    17 public class BodyServlet extends HttpServlet {
    18     private static final long serialVersionUID = 1L;
    19        
    20     /**
    21      * @see HttpServlet#HttpServlet()
    22      */
    23     public BodyServlet() {
    24         super();
    25         // TODO Auto-generated constructor stub
    26     }
    27 
    28     /**
    29      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    30      */
    31     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    32         // TODO Auto-generated method stub
    33         
    34     }
    35 
    36     private String readBody(HttpServletRequest request) throws IOException {
    37         // TODO Auto-generated method stub
    38         BufferedReader reader=request.getReader();//取得BufferedReader对象
    39         String input=null;
    40         String requestBody="";
    41         while((input=reader.readLine())!=null){
    42             requestBody=requestBody+input+"<br>";
    43         }
    44         return requestBody;
    45     }
    46 
    47     /**
    48      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    49      */
    50     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    51         // TODO Auto-generated method stub
    52         String body=readBody(request);
    53         PrintWriter out=response.getWriter();//取得响应输出对象
    54         out.println("<html>");
    55         out.println("<head>");
    56         out.println("<title>Servlet BodyView</title>");
    57         out.println("</head>");
    58         out.println("<body>");
    59         out.println(body);
    60         out.println("</body>");
    61         out.println("</html>");
    62         out.close();
    63     }
    64 
    65 }
    BodyServlet
     1 <!DOCTYPE html>
     2 <html>
     3 <head>
     4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     5 <title>Insert title here</title>
     6 </head>
     7 <body>
     8     <form action="body.view" method="post">
     9         名称:<input type="text" name="user"><br>
    10         密码:<input type="password" name="passwd"><br><br>
    11         <input type="submit" name="login" value="送出">
    12     </form>
    13 </body>
    14 </html>
    form.html

     在名称字段上输入“良葛格”,密码上输入“123456”,单机送出按钮,得到:

        

    2.3 getPart()、getParts()取得上传文件

      Servlet3.0中,新增了Part接口,可以方便进行文件上传处理。可以通过request对象的getPart()方法取得Part对象。

      例1:上传文件到指定目录。

     1 package cc.openhome;
     2 
     3 import java.io.FileOutputStream;
     4 import java.io.IOException;
     5 import java.io.InputStream;
     6 import java.io.OutputStream;
     7 
     8 import javax.servlet.ServletException;
     9 import javax.servlet.annotation.MultipartConfig;
    10 import javax.servlet.annotation.WebServlet;
    11 import javax.servlet.http.HttpServlet;
    12 import javax.servlet.http.HttpServletRequest;
    13 import javax.servlet.http.HttpServletResponse;
    14 import javax.servlet.http.Part;
    15 
    16 
    17 /**
    18  * Servlet implementation class PhotoServlet
    19  */
    20 @MultipartConfig(location="e:/java web/workspace/")//设置location属性
    21 @WebServlet("/photo.do")
    22 public class PhotoServlet extends HttpServlet {
    23     private static final long serialVersionUID = 1L;
    24        
    25     /**
    26      * @see HttpServlet#HttpServlet()
    27      */
    28     public PhotoServlet() {
    29         super();
    30         // TODO Auto-generated constructor stub
    31     }
    32 
    33     /**
    34      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    35      */
    36     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    37         // TODO Auto-generated method stub
    38     }
    39 
    40     /**
    41      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    42      */
    43     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    44         // TODO Auto-generated method stub
    45         request.setCharacterEncoding("UTF-8");
    46         Part part=request.getPart("photo");
    47         String filename=getFilename(part);
    48         //writeTo(filename,part);
    49         part.write(filename);//将文件写入location的指定目录
    50     }
    51 
    52 //    private void writeTo(String filename, Part part) throws IOException {
    53 //        // TODO Auto-generated method stub
    54 //        InputStream in=part.getInputStream();
    55 //        OutputStream out=new FileOutputStream("e:/java web/workspace/"+filename);
    56 //        byte[] buffer=new byte[1024];
    57 //        int length=-1;
    58 //        while((length=in.read(buffer))!=-1){
    59 //            out.write(buffer,0,length);
    60 //        }
    61 //        in.close();
    62 //        out.close();
    63 //    }
    64 
    65     private String getFilename(Part part) {
    66         // TODO Auto-generated method stub
    67         String header=part.getHeader("Content-Disposition");
    68         String filename=header.substring(header.indexOf("filename="")+10,header.lastIndexOf("""));
    69         return filename;
    70     
    71     }
    72 
    73 }
    UploadServlet.java
     1 <!DOCTYPE html>
     2 <html>
     3 <head>
     4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" >
     5 <title>Insert title here</title>
     6 </head>
     7 <body>
     8     <form action="photo.do" method="post" enctype="multipart/form-data">
     9         上传相片:<input type="file" name="photo" value="" /><br>
    10         <input type="submit" name="upload" value="上传">
    11     </form>
    12 </body>
    13 </html>
    upload.html

      在Tomcat中必须设置@MultipartConfig标注才能使用getPart()相关API。@MultipartConfig也可以设置属性,比如location。@MultipartConfig(location="c:/workspace"),则上例子就可以修改为:

     1 package cc.openhome;
     2 
     3 import java.io.FileOutputStream;
     4 import java.io.IOException;
     5 import java.io.InputStream;
     6 import java.io.OutputStream;
     7 
     8 import javax.servlet.ServletException;
     9 import javax.servlet.annotation.MultipartConfig;
    10 import javax.servlet.annotation.WebServlet;
    11 import javax.servlet.http.HttpServlet;
    12 import javax.servlet.http.HttpServletRequest;
    13 import javax.servlet.http.HttpServletResponse;
    14 import javax.servlet.http.Part;
    15 
    16 
    17 /**
    18  * Servlet implementation class PhotoServlet
    19  */
    20 @MultipartConfig(location="e:/java web/workspace/")//设置location属性
    21 @WebServlet("/photo.do")
    22 public class PhotoServlet extends HttpServlet {
    23     private static final long serialVersionUID = 1L;
    24        
    25     /**
    26      * @see HttpServlet#HttpServlet()
    27      */
    28     public PhotoServlet() {
    29         super();
    30         // TODO Auto-generated constructor stub
    31     }
    32 
    33     /**
    34      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    35      */
    36     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    37         // TODO Auto-generated method stub
    38     }
    39 
    40     /**
    41      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    42      */
    43     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    44         // TODO Auto-generated method stub
    45         request.setCharacterEncoding("UTF-8");
    46         Part part=request.getPart("photo");
    47         String filename=getFilename(part);
    48         //writeTo(filename,part);
    49         part.write(filename);//将文件写入location的指定目录
    50     }
    51 
    52 //    private void writeTo(String filename, Part part) throws IOException {
    53 //        // TODO Auto-generated method stub
    54 //        InputStream in=part.getInputStream();
    55 //        OutputStream out=new FileOutputStream("e:/java web/workspace/"+filename);
    56 //        byte[] buffer=new byte[1024];
    57 //        int length=-1;
    58 //        while((length=in.read(buffer))!=-1){
    59 //            out.write(buffer,0,length);
    60 //        }
    61 //        in.close();
    62 //        out.close();
    63 //    }
    64 
    65     private String getFilename(Part part) {
    66         // TODO Auto-generated method stub
    67         String header=part.getHeader("Content-Disposition");
    68         String filename=header.substring(header.indexOf("filename="")+10,header.lastIndexOf("""));
    69         return filename;
    70     
    71     }
    72 
    73 }
    UploadServlet.java

      如果有多个文件要上传,可以使用getParts方法,这回返回一个Collection<Part>,其中是每个上传文件的part对象。  

      例子2:上传多个文件

      1 package cc.openhome;
      2 
      3 import java.io.DataInputStream;
      4 import java.io.FileOutputStream;
      5 import java.io.IOException;
      6 import java.io.UnsupportedEncodingException;
      7 
      8 import javax.servlet.ServletException;
      9 import javax.servlet.annotation.WebServlet;
     10 import javax.servlet.http.HttpServlet;
     11 import javax.servlet.http.HttpServletRequest;
     12 import javax.servlet.http.HttpServletResponse;
     13 
     14 
     15 
     16 /**
     17  * Servlet implementation class UploadServlet
     18  */
     19 @WebServlet("/upload.do")
     20 public class UploadServlet extends HttpServlet {
     21     private static final long serialVersionUID = 1L;
     22        
     23     /**
     24      * @see HttpServlet#HttpServlet()
     25      */
     26     public UploadServlet() {
     27         super();
     28         // TODO Auto-generated constructor stub
     29     }
     30 
     31     /**
     32      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     33      */
     34     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     35         // TODO Auto-generated method stub
     36     }
     37 
     38     /**
     39      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     40      */
     41     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     42         // TODO Auto-generated method stub
     43         request.setCharacterEncoding("UTF-8");
     44         //锟斤拷取锟斤拷锟斤拷Body
     45         byte[] body=readBody(request);
     46         //取锟斤拷锟斤拷锟斤拷Body锟斤拷锟捷碉拷锟街凤拷锟斤拷锟斤拷示
     47         String textBody=new String(body,"UTF-8");
     48         //取锟斤拷锟较达拷锟斤拷锟斤拷锟斤拷锟侥硷拷
     49         String filename=getFilename(textBody);
     50         //取锟斤拷锟侥硷拷锟斤拷始锟斤拷锟斤拷锟轿伙拷锟�
     51         Position p=getFilePosition(request,textBody);
     52         //锟斤拷锟斤拷锟斤拷募锟�
     53         writeTo(filename,body,p);
     54     }
     55 
     56     private void writeTo(String filename, byte[] body, Position p) throws IOException {
     57         // TODO Auto-generated method stub
     58         FileOutputStream fileOutputStream=new FileOutputStream("e:/java web/workspace/"+filename);
     59         fileOutputStream.write(body,p.begin,(p.end-p.begin));
     60         fileOutputStream.flush();
     61         fileOutputStream.close();
     62     }
     63 
     64     private Position getFilePosition(HttpServletRequest request, String textBody) throws UnsupportedEncodingException {
     65         // TODO Auto-generated method stub
     66         //取得实际上传的文件
     67         String contentType=request.getContentType();
     68         String boundaryText=contentType.substring(contentType.lastIndexOf("=")+1,contentType.length());
     69         //取得实际上传的文件
     70         int pos=textBody.indexOf("filename="");
     71         pos=textBody.indexOf("
    ",pos)+1;
     72         pos=textBody.indexOf("
    ",pos)+1;
     73         pos=textBody.indexOf("
    ",pos)+1;
     74         int boundaryLoc=textBody.indexOf(boundaryText,pos)-4;
     75         int begin=((textBody.substring(0,pos)).getBytes("GBK")).length;
     76         int end=((textBody.substring(0,boundaryLoc)).getBytes("GBK")).length;
     77         return new Position(begin, end);
     78     }
     79 
     80     private String getFilename(String textBody) {
     81         // TODO Auto-generated method stub
     82         String filename=textBody.substring(textBody.indexOf("filename="")+10);
     83         filename=filename.substring(0,filename.indexOf("
    "));
     84         filename=filename.substring(filename.lastIndexOf("\")+1,filename.indexOf("""));
     85         return filename;        
     86     }
     87 
     88     private byte[] readBody(HttpServletRequest request) throws IOException {
     89         // TODO Auto-generated method stub
     90         int formatDataLength=request.getContentLength();
     91         DataInputStream dataStream=new DataInputStream(request.getInputStream());//取锟斤拷InputStraem锟侥讹拷锟斤拷
     92         byte body[]=new byte[formatDataLength];
     93         int totalBytes=0;
     94         while(totalBytes<formatDataLength){
     95             int bytes=dataStream.read(body,totalBytes,formatDataLength);
     96             totalBytes+=bytes;
     97         }
     98         return body;
     99     }
    100 
    101 }
    102 class Position{
    103     int begin;
    104     int end;
    105     public Position(int begin,int end) {
    106         // TODO Auto-generated constructor stub
    107         this.begin=begin;
    108         this.end=end;
    109     }
    110 }
    UploadServlet2.java
     1 <!DOCTYPE html>
     2 <html>
     3 <head>
     4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" >
     5 <title>Insert title here</title>
     6 </head>
     7 <body>
     8     <form action="upload1.do" method="post" enctype="multipart/form-data">
     9         文件1:<input type="file" name="file1" value="" /><br>
    10         文件2:<input type="file" name="file2" value="" /><br>
    11         文件3:<input type="file" name="file3" value="" /><br><br>
    12         <input type="submit" name="upload" value="上传"/>
    13     </form>
    14 </body>
    15 </html>
    multi-upload.html

    2.4 使用RequestDispatcher调派请求

      在web应用程序中,经常需要多个Servlet来完成请求。例如,将另一个Servlet的请求处理流程包含进来,或者将请求转发forward给别的servlet处理。可以使用:

        RequestDispatcher dispatcher = request.getRequestDispatcher("some.do");

      (1)、使用include()方法

        将另一个servlet的操作流程包括至目前的servlet操作之中。

        例1:include方法

     1 package cc.openhome;
     2 
     3 import java.io.IOException;
     4 import java.io.PrintWriter;
     5 
     6 import javax.servlet.RequestDispatcher;
     7 import javax.servlet.ServletException;
     8 import javax.servlet.annotation.WebServlet;
     9 import javax.servlet.http.HttpServlet;
    10 import javax.servlet.http.HttpServletRequest;
    11 import javax.servlet.http.HttpServletResponse;
    12 
    13 /**
    14  * Servlet implementation class Some
    15  */
    16 @WebServlet("/some.view")
    17 public class Some extends HttpServlet {
    18     private static final long serialVersionUID = 1L;
    19        
    20     /**
    21      * @see HttpServlet#HttpServlet()
    22      */
    23     public Some() {
    24         super();
    25         // TODO Auto-generated constructor stub
    26     }
    27 
    28     /**
    29      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    30      */
    31     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    32         // TODO Auto-generated method stub
    33         PrintWriter out=response.getWriter();
    34         out.println("Some do one...");
    35         RequestDispatcher dispatcher=request.getRequestDispatcher("other.view?data=123456");
    36         dispatcher.include(request, response);
    37         out.println("Some do two...");
    38         out.close();
    39     }
    40 
    41     /**
    42      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    43      */
    44     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    45         // TODO Auto-generated method stub
    46     }
    47 
    48 }
    some.java
     1 package cc.openhome;
     2 
     3 import java.io.IOException;
     4 import java.io.PrintWriter;
     5 
     6 import javax.servlet.ServletException;
     7 import javax.servlet.annotation.WebServlet;
     8 import javax.servlet.http.HttpServlet;
     9 import javax.servlet.http.HttpServletRequest;
    10 import javax.servlet.http.HttpServletResponse;
    11 
    12 /**
    13  * Servlet implementation class OtherServlet
    14  */
    15 @WebServlet("/other.view")
    16 public class OtherServlet extends HttpServlet {
    17     private static final long serialVersionUID = 1L;
    18        
    19     /**
    20      * @see HttpServlet#HttpServlet()
    21      */
    22     public OtherServlet() {
    23         super();
    24         // TODO Auto-generated constructor stub
    25     }
    26 
    27     /**
    28      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    29      */
    30     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    31         // TODO Auto-generated method stub
    32         PrintWriter out=response.getWriter();
    33         out.println("Other do one..");
    34     }
    35 
    36     /**
    37      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    38      */
    39     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    40         // TODO Auto-generated method stub
    41     }
    42 
    43 }
    othersome.java

       运行结果:网页上看到响应顺序是Some do one ... Other do one... Some do two.. .    

      (2)、使用forward()方法

        将处理请求转发给别的Servlet。

        例2:forwod方法

     1 package cc.openhome;
     2 
     3 import java.io.IOException;
     4 
     5 import javax.servlet.ServletException;
     6 import javax.servlet.annotation.WebServlet;
     7 import javax.servlet.http.HttpServlet;
     8 import javax.servlet.http.HttpServletRequest;
     9 import javax.servlet.http.HttpServletResponse;
    10 
    11 import com.sun.javafx.sg.prism.NGShape.Mode;
    12 import com.sun.media.sound.ModelAbstractChannelMixer;
    13 
    14 /**
    15  * Servlet implementation class HelloController
    16  */
    17 @WebServlet("/hello.do")
    18 public class HelloController extends HttpServlet {
    19     private static final long serialVersionUID = 1L;
    20     private HelloModel model=new HelloModel();
    21     /**
    22      * @see HttpServlet#HttpServlet()
    23      */
    24     public HelloController() {
    25         super();
    26         // TODO Auto-generated constructor stub
    27     }
    28 
    29     /**
    30      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    31      */
    32     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    33         // TODO Auto-generated method stub
    34         String name=request.getParameter("user");//收集请求参数
    35         String message=model.doHello(name);//委托HelloModel对象处理
    36         request.setAttribute("message", message);//将结果信息设置至请求对像成属性
    37         request.getRequestDispatcher("hello1.view").forward(request, response);
    38     }
    39 
    40     /**
    41      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    42      */
    43     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    44         // TODO Auto-generated method stub
    45         
    46     }
    47 
    48 }
    HelloController.java
     1 package cc.openhome;
     2 
     3 import java.util.*;
     4 
     5 
     6 
     7 public class HelloModel {
     8     private Map<String, String>messages=new HashMap<String,String>();
     9     public HelloModel(){
    10         messages.put("caterpillar", "Hello");
    11         messages.put("Justin", "Welcome");
    12         messages.put("momor", "Hi");
    13     }
    14     public String doHello(String user){
    15         String message=messages.get(user);
    16         return message+", "+user+"!";
    17     }
    18 }
    HelloModel.java 
     1 package cc.openhome;
     2 
     3 import java.io.IOException;
     4 import javax.servlet.ServletException;
     5 import javax.servlet.annotation.WebServlet;
     6 import javax.servlet.http.HttpServlet;
     7 import javax.servlet.http.HttpServletRequest;
     8 import javax.servlet.http.HttpServletResponse;
     9 
    10 /**
    11  * Servlet implementation class HelloView
    12  */
    13 @WebServlet("/hello1.view")
    14 public class HelloView extends HttpServlet {
    15     private static final long serialVersionUID = 1L;
    16     private String htmlTemplate=
    17               "<html>"
    18             +     "<head>"
    19             +        "<meta http-equiv='Content-Type'"
    20             +            "content='text/html; charset=UTF-8'>"
    21             +        "<title>%s</title>"
    22             +    "</head>"
    23             +    "<body>"
    24             +        "<h1>%s</h1>"
    25             +    "</body>"
    26             +"</html>"; 
    27     /**
    28      * @see HttpServlet#HttpServlet()
    29      */
    30     public HelloView() {
    31         super();
    32         // TODO Auto-generated constructor stub
    33     }
    34 
    35     /**
    36      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    37      */
    38     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    39         // TODO Auto-generated method stub
    40         String user=request.getParameter("user");//取得请求参数
    41         String message=(String) request.getAttribute("message");//取得请求属性
    42         String html=String.format(htmlTemplate, user,message);//产生html结果
    43         response.getWriter().print(html);//输出html结果
    44         
    45     }
    46 
    47     /**
    48      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    49      */
    50     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    51         // TODO Auto-generated method stub
    52     }
    53 
    54 }
    HelloView.java

      运行结果:(Model2架构)

        

  • 相关阅读:
    Unity3D_08_XML文件创建,读取,修改,添加
    Unity3D_07_日志、文本打印
    Unity3D_06_根据Transform、GameObject和Tag获取子对象集合
    Unity3D_05_理解Unity的新GUI系统(UGUI)
    Unity3D_04_GameObject,Component,Time,Input,Physics
    Unity3D_03_代码及效率优化总结
    Unity3D_02_基类MonoBehaviour/自带函数以及脚本执行的生命周期
    Unity3D_01_各种寻找GameObject方法
    人工智能相关
    ubuntu 安装 typora
  • 原文地址:https://www.cnblogs.com/liuzhongfeng/p/6105478.html
Copyright © 2011-2022 走看看