HttpServletRequest接口實例化的使用

HttpServletRequ接口的使用和jsp內置對象的request對象很是相似,request對象其實html

就是HttpServletRequest接口的一個實例,不過氣實例化的過程是自動的,無須自定義。java

如下示例達到的效果爲:經過一個HttpServletRequest接口的實利化對象設置並取得瀏覽器

request範圍屬性的示例。jsp

RequestDemo.java測試

 1 package com.mhb;
 2 
 3 import java.io.IOException;
 4 import java.io.PrintWriter;
 5 
 6 import javax.servlet.ServletException;
 7 import javax.servlet.http.HttpServlet;
 8 import javax.servlet.http.HttpServletRequest;
 9 import javax.servlet.http.HttpServletResponse;
10 
11 public class RequestDemo extends HttpServlet {
12 
13 public void init() throws ServletException {
14 }
15 public void doGet(HttpServletRequest request, HttpServletResponse response)
16 throws ServletException, IOException {
17 
18 //設置輸出內容的格式和編碼
19 response.setContentType("text/html;charset=gb2312");
20 PrintWriter out = response.getWriter();
21 
22 //存儲在request範圍內
23 request.setAttribute("name", "測試者");
24 //取得request範圍name的屬性
25 String name = (String) request.getAttribute("name");
26 
27 out.println("<html>");
28 out.println("<body>");
29 out.println("name:"+name);
30 out.println("</body>");
31 out.println("</html>");
32 
33 }
34 public void doPost(HttpServletRequest request, HttpServletResponse response)
35 throws ServletException, IOException {
36 }
37 public void destroy() {
38 super.destroy();
39 }
40 }

瀏覽器顯示:編碼

相關文章
相關標籤/搜索