Servlet高级

1. 获取初始化参数

在web.xml中配置Servlet时,可以配置一些初始化参数。而在Servlet中可以通过ServletConfig接口提供的方法来取得这些参数。

index.jsp

 1 <%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11
12     <title>My JSP ‘index.jsp‘ starting page</title>
13     <meta http-equiv="pragma" content="no-cache">
14     <meta http-equiv="cache-control" content="no-cache">
15     <meta http-equiv="expires" content="0">
16     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
17     <meta http-equiv="description" content="This is my page">
18     <!--
19     <link rel="stylesheet" type="text/css" href="styles.css">
20     -->
21   </head>
22
23   <body>
24     <h1>获取初始化参数演示案例</h1>
25     <hr>
26     <a href = "servlet/GetInitParam">获取初始化参数</a>
27   </body>
28 </html>

web.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app version="2.5"
 3     xmlns="http://java.sun.com/xml/ns/javaee"
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 6     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 7   <display-name></display-name>
 8   <servlet>
 9     <servlet-name>GetInitParam</servlet-name>
10     <servlet-class>servlet.GetInitParam</servlet-class>
11     <init-param>
12       <param-name>username</param-name>
13       <param-value>admin</param-value>
14     </init-param>
15     <init-param>
16       <param-name>password</param-name>
17       <param-value>123456</param-value>
18     </init-param>
19   </servlet>
20
21   <servlet-mapping>
22     <servlet-name>GetInitParam</servlet-name>
23     <url-pattern>/servlet/GetInitParam</url-pattern>
24   </servlet-mapping>
25   <welcome-file-list>
26     <welcome-file>index.jsp</welcome-file>
27   </welcome-file-list>
28 </web-app>

GetInitParam.java

 1 package servlet;
 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 GetInitParam extends HttpServlet {
12
13     private String username; // 用户名
14     private String password; // 密码
15
16     public String getUsername() {
17         return username;
18     }
19
20     public void setUsername(String username) {
21         this.username = username;
22     }
23
24     public String getPassword() {
25         return password;
26     }
27
28     public void setPassword(String password) {
29         this.password = password;
30     }
31
32     /**
33      * The doGet method of the servlet. <br>
34      *
35      * This method is called when a form has its tag value method equals to get.
36      *
37      * @param request the request send by the client to the server
38      * @param response the response send by the server to the client
39      * @throws ServletException if an error occurred
40      * @throws IOException if an error occurred
41      */
42     public void doGet(HttpServletRequest request, HttpServletResponse response)
43             throws ServletException, IOException {
44
45         doPost(request, response);
46     }
47
48     /**
49      * The doPost method of the servlet. <br>
50      *
51      * This method is called when a form has its tag value method equals to post.
52      *
53      * @param request the request send by the client to the server
54      * @param response the response send by the server to the client
55      * @throws ServletException if an error occurred
56      * @throws IOException if an error occurred
57      */
58     public void doPost(HttpServletRequest request, HttpServletResponse response)
59             throws ServletException, IOException {
60
61         response.setContentType("text/html; charset=UTF-8");
62         PrintWriter out = response.getWriter();
63         out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
64         out.println("<HTML>");
65         out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
66         out.println("  <BODY>");
67         out.println("    <h2>账号:" + this.getUsername() + "</h2>");
68         out.println("    <h2>密码:" + this.getPassword() + "</h2>");
69         out.println("  </BODY>");
70         out.println("</HTML>");
71         out.flush();
72         out.close();
73     }
74
75     public void init() throws ServletException {
76         this.setUsername(this.getInitParameter("username"));
77         this.setPassword(this.getInitParameter("password"));
78     }
79 }

2. MVC设计模式

3. Model2简介

4. 阶段项目

时间: 2024-08-05 15:33:43

Servlet高级的相关文章

&lt;&lt;&lt; web里面Servlet高级应用的基础介绍

Servlet中的页面跳转?两种方式,实现跳转:内部跳转(请求转发).外部跳转(重定向)内部跳转(请求转发)特点:在服务器内部完成页面之间的跳转:请求只有一次:浏览器地址不会改变.request.getRequestDispatcher(“路径”).forward(reuqest,response);request.getRequestDispatcher(“路径”).include(reuqest,response); 外部跳转(重定向)特点:通知客户端重新发送请求:请求次数为二次:浏览器地址

Servlet高级应用---Servlet与缓存

一]设置缓存文件的有效日期        重点方法:            HttpServletRequest类:                    1>String getRequestURI();//获取客户端地址栏的路径值            HttpServletResponse类:                    2>void SetDateHeader(String name, long endTime);                            nam

Servlet高级部分Filter(过滤器)

一:Filter称之为"过滤器",用在Servlet外,对request和response进行修改.它是AOP(面向切面编程思想的一种体现),Filter中有一个FilterChain的概念,称之为"过滤器链".一个FilterChain包含了多个Filter,一个请求到达Servlet或者一个响应到达客户端都要经过FilterChain串联起来的所有Filter.Filter的处理流程如下[Filter于Servlet的耦合性不高,可以随意装装载和卸载]: *:一

Java Web基础(二)(Servlet编写与配置)

下面的内容都是基础的Servlet知识,如果你想打好基础,就别急着学习JSP,而是从Servlet开始学习.因为JSP页面最终会被转译为Servlet,掌握了Servlet,JSP也就学会了一半,而且在写JSP页面时也能更准确地定位错误. 开发环境的搭建我就不说了,百度一搜一大堆,说一下我的开发环境:MyEclipse + Tomcat 7.0 + JDK 7 编写一个基本的Servlet 我们使用最原始的方式编写一个Servlet,首先遇到建一个类,然后继承HttpServlet,并实现其中的

eclipse开发Servlet

#eclipse 开发Servlet1.新建工程.百度了下,只有在你的应用的WebContent/WEB-INF/lib加上库,才会显示.开发与配置比较简单,但是eclipse部署应用到自己的tomcat上出现问题,查阅了网上的一些资料. ##eclipse配置Tomcat服务器server locations的方法(转之http://yedward.net/?id=303)最近放弃了使用Myeclipse,转而使用eclipse作为开发工具,确实Myeclipse集成了太多东西,使得开发人员的

Servlet---JavaWeb技术的核心基础,JavaWeb框架的基石(二)

一.Servlet之Request Web服务器会对收到的每一次客户端http请求分别创建一个用于代表请求的request对象和代表响应的response对象.要获取客户端提交的数据需通过request,要想容器输出数据需通过response. 1.HttpServletRequest HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,开发人员通过这个对象的方法可以会的客户的相关信息. 2.Request

Servilet初步

以http://locahost:8080/......开头,或者以/开头,都是绝对路径以路径开头:相对路径 路径/路径 Servlet执行流程:(只用自己编写执行的代码,执行的细节全是tomcat封装的) 1:浏览器输入URL(回车) 找到服务器端的Tomcat 2:tomcat解析web.xml文件,根据URL中项目名后面的路径找到对应的class <servlet> <servlet-name>first</servlet-name> //自己给自己的servle

如何自学 Java 开发

如何自学 Java 开发? 568赞同反对,不会显示你的姓名 李艾米IT路上学习 568 人赞同 Java Web前端技术 HTML 入门视频课程 1 HTML 简介 2 HTML基本结构[ 3 HTML的BODY标签以及颜色的设定 4 HTML之br标签 5 HTML之p标签 6 HTML之pre标签. 7 HTML之center 8 HTML之引文标签 9 HTML之hr 10 HTML之address 11 HTML之meta 标签 12 HTML之特殊字符 13 HTML之注释 14 H

【2014,上半年是一个转变】

曾以为这是一个无比平凡的一年,可是年初就有了一个不平凡的开始. 3月完成了自考的毕业设计.学了GossipGirl和J2SE: 4月结束了自考的最后两项:操作系统,三级网络,学了希赛视频: 5月顺利通过软考,期间学了设计模式.做题和进京赶考,并开始了BS: 6月半顺利大学毕业,收获了老师.同学.N多朋友的情谊. 毕业寄语:大学缘起的地方 毕业前 ============================================================================