javaServlet学习笔记

JavaWeb配置
Tomcat配置
tomcat可以在bin目录下直接在setclasspath.ba里配置
set JAVA_HOME
set JRE_Home
tomcat端口号默认是8080

在conf目录下server.xml可以进行端口

创建好项目后发现jsp页面有错误

把这里的tomcat服务删除,从新引入

Servlet web.xml配置

First
base.First

user
admin

1


First
/First

Servlet 接口
方法

1、 public void init(ServletConfig config) throws ServletException;

2、 public ServletConfig getServletConfig();

3、 public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException;

4、 public String getServletInfo();

5、 public void destroy();
6、 说明:
1、构造方法,最先被执行,意味着创建新的实例,且只会别执行一次,意味着Servlet是单例的,在整个Servlet容器中每个具体的Servlet实现类都只有一个实例
2、init:构造方法之后马上被执行,且只执行一次,作用是做初始化,虽然构造方法也能做初始化,但init(ServletConfig arg0)是带了参数回来的,作用更大
3、service:被多次调用,每次浏览器刷新请求都会调用一次service方法,实际用于对请求的响应。
4、destroy:只被调用一次,在Servlet所在的web应用被卸载前执行,用于释放Servlet占用的计算机资源
——web.xml中Servlet映射配置中整数,这个配置的整数若为0或正整数时表示web应用启动的时候,自动执行对应的Servlet,整数越小,越优先执行,只执行一次随web应用启动时。若为负数,则会在第一次请求时被执行对象的Serviet。

ServletConfig接口
方法
Private ServletConfig config;
Private ServletContext context;
1、 public String getServletName();
获取当前servlet在web.xml中配置的名字

2、 public ServletContext getServletContext();
this.context = config.getServletContext();
获取当前ServletContext对象,名称的初始化参数的值

3、 public String getInitParameter(String name);
获取当前servlet初始化参数的名字(由配置对象调用)

4、 public Enumeration getInitParameterNames();

获取当前servlet所有初始化参数的名字组成枚举

ServletContext接口(非常重要)
接口说明
WEB容器在启动时,它会为每个web应用都创建一个对应的ServletContext对象,他代表当前的web应用
可以通过ServletConfig.getServletContext方法获得ServletContext对象

由于每一个web应用中所有的servlet共享一个ServletContext对象,因此Servlet对象之间可以通过ServletContext对象来实现通讯。(也就是说在同一个web服务器上运行的项目之间可以相互访问ServletContext对象信息)

ServletContext对象通常也被称之为context域对象
ServletContext的应用
1、作为域对象可以在整个web应用范围内共享数据
这里涉及到一些概念:
——域对象:在一个可以被看见的范围内共享数据
——作用范围:整个web应用范围内共享数据
——生命周期:当服务器启动web应用加载口创建出ServletContext对象后,域产生。当web应用别移除容器或服务器关闭,随着web应用的销毁域销毁。
方法
1、 public void setAttribute(String name, Object object);
为ServletContext对象设置属性内容
2、public Object getAttribute(String name);
获取ServletContext对象属性设置的内容
3、public String getRealPath(String path);
获取文件路径(方法里传入文件名可以得到对应的文件路径)
Properties poperties = new Properties();
System.out.println(servletContext.getRealPath(“web.properties”));
Reader rea = new FileReader(servletContext.getRealPath(“web.properties”));
poperties.load(rea);
System.out.println(poperties.getProperty(“psd”));

4、public String getContextPath();
获取当前web应用的名称
可以用域对象调用此方法
5、public String[] getParameterValues(String name);
获取参数值
(ServletRequest req, ServletResponse res)
String[] str = req. getParameterValues(“aihao”);

ServletContext配置web.xml

name
value

设置请求和响应的编码以及响应的类型
req.setCharacterEncoding(“UTF-8”);
res.setCharacterEncoding(“UTF-8”);
res.setContentType(“text/html;charset=utf-8”);

ServletRequest接口
方法
1、public String getParameter(String name);
获取请求参数(由请求对象调用)
2、public String getScheme();
获取请求的方案(例如:http)
3、public String getServerName();
取得请求发送到的服务器的主机名(例如:localhost)
4、public int getServerPort();
取得请求端口号
5、public void setCharacterEncoding(String env)
throws java.io.UnsupportedEncodingException;
设置请求内容的字符编码
6、public String getCharacterEncoding();
取得请求的字符编码
7、public String getProtocol();
8、public String[] getParameterValues(String name);
取得请求参数的值以数组的形式返回
9、public boolean isSecure();
判断通信通道是否安全(https就是安全的)
取得请求的协议以及版本号

ServletResponse接口
方法
1、public void setCharacterEncoding(String charset);
设置响应内容的字符编码
2、public String getContentType();
取得响应内容的类型及字符编码(由响应对象调用)
3、public String getCharacterEncoding();
取得响应内容的编码

GenericServlet
HttpServlet
真正开发的时候,我们自定义的Servlet来继承HttpServlet这个类,然后复写doGet()或者doPost()方法
public abstract class HttpServlet extends GenericServlet{}
public abstract class GenericServlet implements Servlet, ServletConfig, java.io.Serializable {}
因为HttpServlet继承了GenericServlet抽象类
GenericServlet实现了Servlet,ServletConfig接口
JSP
JSP全名为Java Serve Pages,中文名叫java服务器页面,其根本是一个简化的Servlet

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值