Servlet API
2018-03-30 14:28:53 87 举报
AI智能生成
Servlet常用 API 仅用于帮助学习理解Servlet机制和协助记忆。 关于所有涉及到的知识详细信息,请参考官方手册。
作者其他创作
大纲/内容
Servlet 生命周期和工作原理
JSP
java代码
JSP 表达式
<p>The square root of 5 is <%=Math.sqrt(5)%></p>
JSP 小脚本
<% for(User user:users){ %>
<tr>
<td>
<%=user.getId()%>
</td>
</tr>
<%
}
%>
<tr>
<td>
<%=user.getId()%>
</td>
</tr>
<%
}
%>
JSP 声明
<%!
public String getResult(){
//......
}
%>
public String getResult(){
//......
}
%>
指令
page指令
<%@ page import="java.util.*,java.text.*"%>
<%@page contentType="text.html;charset=utf-8"%>
<%@page pageEncoding="utf-8"%>
include指令
<%@include file="header.html"%>
taglib指令
隐含对象
输入输出对象
request
.getAttribute(String name):Object-ServletRequest
.getContextPath():String-HttpServletRequest
response
out
作用域通信对象
session
application
pageContext
Servlet对象
page
config
异常对象
exception
注释
<!-- 注释内容 -->
<%-- 注释内容 --%>
JSP标签
EL表达式
访问Bean的属性
${对象名.属性名}
${对象名["属性名"]}
做一些简单的运算
${empty str1}
读取请求参数值
${param.name}
${paramValues.interest}
JSTL(java standard tag lib)
if标签
choose标签
forEach标签
自定义标签
MVC(Model 模型 View 视图 Controller 控制器)
java反射
Class clazz = Class.forName(className);
Object obj = clazz.newInstance();
Method[] methods = clazz.getDeclareMethods();
for(Method mh : methods){
Demo demo = mh.getAnnotation(Demo.class);
if(demo != null){
//获得注解的属性值
String value = demo.value();
//有@Demo注解,则执行
mh.invoke(obj);
}
}
Demo demo = mh.getAnnotation(Demo.class);
if(demo != null){
//获得注解的属性值
String value = demo.value();
//有@Demo注解,则执行
mh.invoke(obj);
}
}
java注解
@Retention(RetentionPolicy.RUNTIME)
public @interface Demo{
public String value();
}
public @interface Demo{
public String value();
}
@Demo("date")
public String c1(){
System.out.println("c2的方法");
return "hello c2";
}
public String c1(){
System.out.println("c2的方法");
return "hello c2";
}
javax.servlet
public interface Servlet
.service(HttpServletRequest req,HttpServletResponse res):void-Servlet
.destroy():void-Servlet
.init(ServletConfig config):void-Servlet
Public abstract class GenericServlet extends java.long.Object
.init(ServletConfig config):void-GenericServlet
.init():void-GenericServlet
.getInitParameter(String name):String-GenericServlet
.getServletConfig():ServletConfig-GenericServlet
javax.servlet.http
Public abstract class HttpServlet extends GenericServlet
.doGet(HttpServletRequest req,HttpServletResponse resp):void-HttpServlet
.doPSOT(HttpServletRequest req,HttpServletResponse resp):void-HttpServlet
.service(HttpServletRequest req,HttpServletResponse resp):void-HttpServlet
Public interface HttpServletRequest extends ServletRequest
设置解析方式
.setCharacterEncoding(String env):void-ServletRequest
请求行
消息头
消息正文
.getParameter(String name):String-ServletRequest
.getParametervalues(Sting name):String[]-ServletRequest
转发
.setAttribute(String name,Object obj):void-ServletRequest
.getRequestDispatcher(String uri):RequestDispatcher-ServletRequest
.forward(request,response):void-RequestDispatcher
.getAttribute(String name):Object-ServletRequest
Public interface HttpServletResponse extends ServletResponse
状态行
.sendRedirect(String location):void-HttpServletResponse
响应头
.setContentType(String type):void-ServletResponse
.setContentLength(int len):void-ServletResponse
.setCharacterEncoding(String charset):void-ServletResponse
响应正文
.getWriter():PrintWriter-ServletResponse
.println():void-PrintWriter
.flush():void-PrintWriter
.close():void-PrintWriter
状态管理
public class Cookie extends Object
添加Cookie
Cookie c = new Cookie(String name,.String value);
response.addCookie(c):void-HttpServletResponse
解析Cookie
request.getCookies():Cookie[]-HttpServletRequest
设置Cookie
编码问题
new Cookie(String name,URLEncoder.encode(String value,"utf-8"));
生存时间
.setMaxAge(int expiry):void-Cookie
.getMaxAge():int-Cookie
路径问题
.setPath(String path):void-Cookie
.getPath():String-Cookie
public interface HttpSession
添加Session
HttpSession s = request.getSession(boolean create);
HttpSession s = request.getSession();
绑定数据
.setAttribute(String name,Object obj):void-HttpSession
.getAttribute(String name):Object-HttpSession
.remoceAttribute(String name):void-HttpSession
Session超时
<session-config>-service/web.xml
.setMaxInactiveInterval(int seconds):void-HttoSession
删除session
.invalidate():void-HttpSession
session验证
step1.登录成功之后,在session对象上绑订数据。
step2.当用户访问一些需要保护的资源,进行session验证
利用session实现验证码
示意图
servlet上下文
绑定数据
全局的初始化参数
过滤和监听工具
监听器
生命周期相关
ServletRequestListener
HttpSessionListener
ServletContextListener
数据绑定相关
ServletRequestAttributeListener
HttpSessionAttributeListener
ServletContextAttributeListener
过滤器
public class CommentFilter implements Filter
.doFilter(ServletRequest arg0,ServletResponse arg1,FilterChain arg2):void-Filter
.init(FilterConfig arg0):void-Filter
.destroy():void-Filter
0 条评论
下一页