§
§ 实验支持环境设备需求
§ 软件支持工具
Java开发工具
Eclipse SDK Version: 3.2.2 Build id: M20070212-1330
J2EE开发工具
MyEclipse Version: 5.1.1 GA
JAVA运行环境工具
JDK(java) Version: 1.5.0_09
Java(TM) 2 Runtime Environment(JRE)
Version: build 1.5.0 Standard Edition
Java HotSpot(TM) Client VM
Version: build 1.5.0_09-b03
mixed mode)
JSP容器
Tomcat Version: 5.5.25 apache-tomcat-5.5.25
§ 实验步骤
1)File->New->Servlet,Config package path:org.luonix.servlets and Servlet Name:MyServlet,then next->finished.
注意:Servlet MyServlet必须继承HttpServlet类(或继承抽象类GenericServlet或扩展接口Servlet)
2)配置web.xml文件
<servlet>
<description>
This is the description of my J2EE component
description>
<display-name>
This is the display name of my J2EE component
display-name>
<servlet-name>MyServletservlet-name>
<servlet-class>org.luonix.servlets.MyServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>MyServletservlet-name>
<url-pattern>/servlet/MyServleturl-pattern>
servlet-mapping>
3)根据客户端的请求方式实现doXXX(HttpServletRequest request, HttpServletResponse response)或service(HttpServletRequest request, HttpServletResponse response)方法。
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("");
out.println("");
out.println("
out.println(" ");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println("Hello World");
out.println(" ");
out.println("");
out.flush();
out.close();
}
4)部署WEB工程到TOMCAT服务器中
5)运行测试
打开浏览器,输入http://127.0.0.1:8080/j2ee-exp/servlet/MyServlet
This is class org.luonix.servlets.MyServlet, using the GET method Hello World
6)在配置文件中添加初始化参数
<init-param>
<param-name>nameparam-name>
<param-value>罗小勇param-value>
init-param>
<init-param>
<param-name>englishnameparam-name>
<param-value>Xiaoyong Luoparam-value>
init-param>
<init-param>
<param-name>sexparam-name>
<param-value>maleparam-value>
init-param>
<init-param>
<param-name>ageparam-name>
<param-value>22param-value>
init-param>
7)显示配置参数
修改doGet(HttpServletRequest request, HttpServletResponse response)如下:
response.setCharacterEncoding("UTF-8");
out.println("this is my profile:");
out.println(this.getServletConfig().getInitParameter("name"));
out.println(this.getServletConfig().getInitParameter("englishname"));
out.println(this.getServletConfig().getInitParameter("sex"));
out.println(this.getServletConfig().getInitParameter("age"));
java.util.Enumeration params = this.getServletConfig().getInitParameterNames();
for(;params.hasMoreElements();){
out.println(this.getServletConfig().getInitParameter(params.nextElement().toString()));
}
8)部署WEB工程到TOMCAT服务器中,运行测试
打开浏览器,输入http://127.0.0.1:8080/j2ee-exp/servlet/MyServlet
This is class org.luonix.servlets.MyServlet, using the GET method Hello World this is my profile: 罗小勇 Xiaoyong Luo male 22 male 22 Xiaoyong Luo 罗小勇
1) 修改Servlet MyServlet.java,添加如下内容:
out.println("
out.println(" out.println(" out.println(" out.println("account: out.println(" out.println(" out.println(" out.println(" out.println("password: out.println(" out.println(" out.println(" out.println(" out.println(" out.println(" out.println(" out.println("");
");");
");");
");
");
");
");");
");
");
");
");");
");
");
out.println("");
创建一个Servlet Filter:MyFilter,MyFilter.java实现过滤功能,用于判断是否只出现a~z、A~Z和0~9这样的字母,其中doFilter(ServletRequest request, ServletResponse response,FilterChain filterchain)实现如下:
this.account = request.getParameter("account");
this.password = request.getParameter("password");
javax.servlet.RequestDispatcher dispatcher = null;
boolean isEN = true;
for(int i=0; i<this.account.length(); i++){
if(!(this.account.charAt(i) >= '0' && this.account.charAt(i) <= '9' ||
this.account.charAt(i) >= 'a' && this.account.charAt(i) <= 'z' ||
this.account.charAt(i) >= 'A' && this.account.charAt(i) <= 'Z' ))
isEN = false;
}
if(isEN)
dispatcher = request.getRequestDispatcher("/servlet/EN");
else
dispatcher = request.getRequestDispatcher("/servlet/CN");
dispatcher.forward(request, response);
其中,下画线部分,通过javax.servlet.RequestDispatcher转发器转发,如果出现了中文等类似的字符,则转发到/servlet/CN所对应的Servlet中去,否则转发到/servlet/CN所对应的Servlet中去。
2) Servlet CN.JAVA,EN.JAVA
分别对应于转发后的中文或英文页面。
3) 最终的WEB.xml配置如下:
<servlet>
<description>
This is the description of my J2EE component
description>
<display-name>
This is the display name of my J2EE component
display-name>
<servlet-name>MyServletservlet-name>
<servlet-class>org.luonix.servlets.MyServletservlet-class>
<init-param>
<param-name>nameparam-name>
<param-value>罗小勇param-value>
init-param>
<init-param>
<param-name>englishnameparam-name>
<param-value>Xiaoyong Luoparam-value>
init-param>
<init-param>
<param-name>sexparam-name>
<param-value>maleparam-value>
init-param>
<init-param>
<param-name>ageparam-name>
<param-value>22param-value>
init-param>
servlet>
<servlet>
<description>
This is the description of my J2EE component
description>
<display-name>
This is the display name of my J2EE component
display-name>
<servlet-name>CNservlet-name>
<servlet-class>org.luonix.servlets.CNservlet-class>
servlet>
<servlet>
<description>
This is the description of my J2EE component
description>
<display-name>
This is the display name of my J2EE component
display-name>
<servlet-name>ENservlet-name>
<servlet-class>org.luonix.servlets.ENservlet-class>
servlet>
<filter>
<description>my filterdescription>
<display-name>myfilterdisplay-name>
<filter-name>myfilterfilter-name>
<filter-class>org.luonix.servlets.MyFilterfilter-class>
filter>
<servlet-mapping>
<servlet-name>MyServletservlet-name>
<url-pattern>/servlet/MyServleturl-pattern>
servlet-mapping>
<servlet-mapping>
<servlet-name>CNservlet-name>
<url-pattern>/servlet/CNurl-pattern>
servlet-mapping>
<servlet-mapping>
<servlet-name>ENservlet-name>
<url-pattern>/servlet/ENurl-pattern>
servlet-mapping>
<filter-mapping>
<filter-name>myfilterfilter-name>
<url-pattern>/filter/MyFilterurl-pattern>
filter-mapping>
§ 最终运行结果