第一次课
二.怎样实现servlet
(1)。实现servlet接口
(2)继承GenericServlet
(3)继承HttpServlet(专门针对于http协议的)
A.接口中方法的解释
向外界提供服务的方法,每请求一次,就向外界提供服务一次,但是要注意下面的例子,i
是会递增的,那证明无论请求多少次此servlet都只有一个对象,所以足以证明servlet
是单粒模式的
注意:在servlet被tomcat new出来的时候,就自动创建了ServletConfig这个对象
然后tomcat会到new好的ServletConfig扔到方法内,供方法使用
一般使用的方式:
(设置一个全局的ServletConfig,这样其他的方法就可以使用到这个ServletConfig )
package com.zl.InitParam;
import java.io.IOException;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class Test1 implements Servlet {
//当sertvlet被销毁的时候tomcat调用此方法,servlet在服务器重启或关掉的时候被销毁
public void destroy() {
System.out.println("销毁了");
}
public ServletConfig getServletConfig() {
return null;
}
public String getServletInfo() {
return null;
}
//一般使用的方式:
//(设置一个全局的ServletConfig,这样其他的方法就可以使用到这个ServletConfig )
private ServletConfig config;
public void init(ServletConfig config) throws ServletException {
//初始化方法
//ServletConfig 记录当前这个服务员的信息(比如芳龄多少啊,哪里人士等等)
//ServletConfig其实就是记录了servlet的配置信息
this.config =config;//在这里进行设置(重要)
System.out.println("开始学习了");
//拿到sevlet名字
System.out.println("Servlet名字:"+config.getServletName());
}
int i=0;
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException {
System.out.println("服务了一次"+i+"次");
// ServletRequest 接口的实现类对象封装了从客户端向服务端发送的请求信息
//ServletResponse接口的实现类对象封装了从服务端向客户端响应的信息
//SevletConfig对象得到初始化参数
String encoding = config.getInitParameter("encoding");
res.setContentType(encoding);
}
}
写好了service就可以让服务员挂牌服务了
怎么挂牌:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<init-param>
<param-name>encoding</param-name>
<param-value>text/html;charset=GB18030</param-value>
</init-param>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.accp.servlet.Hello</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern> //注意“/”不能少
</servlet-mapping>
</web-app>
怎么访问呢http://localhost:端口号/项目名/url-pattern配值的参数值
B.使用ServletResponse 向客户端打印输出信息
设置响应的编码
第一种方式:
//res.setContentType("text/html;charset=GB18030");
// 与<%@ page contentType="text/html; charset=GB18030"%>相对应
// 第二种方式:使用配置文件和SevletConfig的配合(更具通用性,很多servlet可以同时使用)
// 配置文件
<init-param>
<param-name>encoding</param-name>
<param-value>text/html;charset=GB18030</param-value>
</init-param>
//SevletConfig对象得到初始化参数
String encoding = config.getInitParameter("encoding");
res.setContentType(encoding);
PrintWriter out = response.getWriter();
out.println("Hello! I am servlet!");//输出英文
out.println("你好,我是servlet")//输出中文
C.使用ServletRequest对象拿到客户端信息
建立一个jsp,写一个表单,用户登录提交,注意action="url-pattern中的参数"(不要/),然
后提交到一个与url-pattern对应的servlet,然后在servlet的service方法中就可以获
取值
注意:中文乱码问题(post)
req.setCharacterEncoding("GB18030"); //解决post方式提交的中文乱码
使用超链接也可以<a href="hello?uname=uu&pwd=uu">登录</a>
(2)继承GenericServlet
重写service方法(必须的)
GenericServlet是一个抽象类实现了Servlet接口
增加了一个ServletConfig的引用
private ServletConfig config;
public void init() throws ServletException {}//新增的
public void init(ServletConfig config) throws ServletException {
this.config = config;
this.init();//调用新增的
}
这样就做就没有以前那么麻烦,所以如果以后要重写初始化方法,重写新增的,如果要重写原来
的有参的init(),要记得在方法里写上这句super.init(config),这是关键;
写一个例子:根据提交的方式输出信息
HttpServletRequest实现了ServletRequest接口
注意:请求是基于http协议的,所以可以强制转换
HttpServletRequest request = (HttpServletRequest)req;
PrintWriter out = res.getWriter();
String method = request.getMethod();
if(method.equalsIgnoreCase("post")){
out.println("hello,post");
}else{
out.println("hello,get");
}
//这种方式过于麻烦于是可以采取第三种方式解决
(3)继承HttpServlet(专门针对于http协议的)
HttpServlet继承了GenericServlet,重写了service方法
注意:service方法中
if (method.equals(METHOD_GET)) {
doGet();
}else if(method.equals(METHOD_POST)){
doGet();
}
所以我们继承HttpServlet只需要实现doGet()和doPost()
原理是:先看自己类里面有没有service方法,子类没有就调用父类的service方法,然后
根据请求方式,再调用子类重写的doGet()和doPost() (多态)
如果同时又重写了service(),出现doGet()和doPost() 失效,是因为自己类的service()
里面并没有去调用doGet()和doPost(),所以要注意在重写service(),记得在方法体内加入
super.service(req, resp);,重新调回父类的
不推荐重新service(),除非万不得已,需要在service()处理数据,记得在方法体内加入
super.service(req, resp);
servlet1见解
最新推荐文章于 2024-10-03 11:38:06 发布