Servlet的生命周期方法:
init()
destroy()
doGet(HttpServletRequest request,HttpServletResponse response)
客户端请求封装在request中,而服务器响应则封装在response中
-------------------------------------------------------------------------------
web.xml的配置(有多个servlet就配置多个servlet 和servlet-mapping标签)
<servlet> <servlet-name>Servelettest1</servlet-name> <servlet-class>com.web.servlet.Servelettest1</servlet-class> //全路径 </servlet> <servlet-mapping> <servlet-name>Servelettest1</servlet-name> <url-pattern>/Servelettest1</url-pattern> //浏览请求别名,前面必须加斜杠 </servlet-mapping>
public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException{ PrintWriter pw = response.getWriter(); //这个输出流是从web服务器到浏览器,打印在web服务器中 pw.print("hello world"); pw.close(); } public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException{ PrintWriter pw = response.getWriter(); pw.print("hello world"); pw.close(); }
在index.jsp中
<a href="./Servelettest1">hello world<a> <form action="Servelettest1" method="post"> //Servelettest1是 <url-pattern>/Servelettest1</url-pattern> <input type="submit" value="提交">
http://localhost:8088/Servelettest1 //Servelettest1是 <url-pattern>/Servelettest1</url-pattern>
-----------------------------------------------------------------------------------
使用request和response
response.getWriter();
//获取客户机参数方法
request.getParameter("username "); //获取参数 index.jsp对应代码段为<a href="Servelettest1?username=boyce">hello world<a>
servlet接受请求封装信息:
public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException{ String username = request.getParameter("username"); //获取用户名 String password = request.getParameter("password"); PrintWriter pw = response.getWriter(); pw.print("hello world"+username+password); pw.close(); }
对应jsp:
<form action="Servelettest1" method="post"> 用户账号:<input type="text" name="username"> 用户密码:<input type="password" name="password"> <input type="submit" value="提交"> </form>
-----------------------------------------------------------
server.xml: //从客户端到服务器的中文乱码
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000" URIEncoding="utf-8"
redirectPort="8443" />
遇到server.xml拒绝访问解决方法:
进去选择编辑,然后选择user权限勾选完全控制即可
//从服务器到客户端的中文乱码
response.setCharacterEncoding("utf-8");
response.setContentType(text/html;charset=utf-8)
servlet实现页面跳转:
package com.web.servlet; import java.io.IOException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class Servelettest2 extends HttpServlet{ public void init(){ } public void destroy(){ } public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException{ String uname = request.getParameter("username"); String password = request.getParameter("password"); System.out.println("Servelet2被调用"); //页面跳转 response.sendRedirect("Servelettest3"); //实现页面的跳转 } }
方法二:
RequestDispatcher rd = request.getRequestDispatcher("Servelettest3"); rd.forward(request,response);
index.jsp:
<form action="Servelettest2" method="post"> 用户账号:<input type="text" name="username"> 用户密码:<input type="password" name="password"> <input type="submit" value="提交"> </form>
doPost中文乱码解决:
public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException{ request.setCharacterEncoding("UTF-8"); //解决请求服务器中文乱码 response.setCharacterEncoding("UTF-8"); //解决服务器到客户端乱码问题 response.setContentType("text/html; charset=utf-8"); //解决服务器到客户端乱码问题 String username = request.getParameter("username"); String password = request.getParameter("password"); PrintWriter pw = response.getWriter(); pw.print("hello world"+username+password); System.out.println(username+password); pw.close(); }
过滤器:
<filter> <filter-name>/filter1</filter-name> <filter-class>com.web.servlet.CharacterFilter</filter-class> </filter> <filter-mapping> <filter-name>/filter1</filter-name> <url-pattern>/*</url-pattern> //星号代表所有的传输都要经过它 </filter-mapping>
package com.web.servlet; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; public class CharacterFilter implements Filter{ @Override public void destroy() { // TODO Auto-generated method stub } @Override public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException { // TODO Auto-generated method stub arg0.setCharacterEncoding("utf-8"); arg1.setCharacterEncoding("utf-8"); arg1.setContentType("text/html; charset=utf-8"); arg2.doFilter(arg0, arg1); } @Override public void init(FilterConfig arg0) throws ServletException { // TODO Auto-generated method stub } }
监听器:
servlet中定义了多种类监听器,它们主要用于监听ServletContext,HttpSession和ServletRequest这三个域对象:
HttpSessionListener接口用于监听HttpSession的创建与销毁
(1)创建一个Session时,sessionCreated(HttpSessionEvent se)方法将会被调用。
(2)销毁一个Session时,sessionDestroy(HttpSessionEvent se)方法将会被调用。
Session域对象创建和销毁时机
创建:yoghurt第一次访问时,服务器创建session
销毁:如果用户30分钟会使用,服务器将会销毁session,我们在web.xml里面也可以诶之session失效时间
<session-config> <session-timeout>1</session-timeout> //设置监听时长为一分钟,若不修改,默认为30分钟 </session-config>
package com.web.servlet; import javax.servlet.http.HttpSessionAttributeListener; import javax.servlet.http.HttpSessionBindingEvent; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; //实现接口HttpSessionListener public class OnlineCounterListener implements HttpSessionListener,HttpSessionAttributeListener{ private int count=0; @Override public void sessionCreated(HttpSessionEvent arg0) { // TODO Auto-generated method stub count++; System.out.println("有新用户访问,当前访问人数为:"+count); } @Override public void sessionDestroyed(HttpSessionEvent arg0) { // TODO Auto-generated method stub count--; System.out.println("有一位退出,当前人数为:"+count); } @Override public void attributeAdded(HttpSessionBindingEvent arg0) { // TODO Auto-generated method stub } @Override public void attributeRemoved(HttpSessionBindingEvent arg0) { // TODO Auto-generated method stub } @Override public void attributeReplaced(HttpSessionBindingEvent arg0) { // TODO Auto-generated method stub } }
总结:
servlet步骤
1.创建servlet类(继承于HttpServlet)
2.三个方法:public void init() {
}
public void destroy() {
}
public void doPost(HttpServletRequest request, HttpServletResponse response){
}
3.通过getParameter 获取jsp中的值
String username = request.getParameter("username");
4.然后调用jdbc或Hibernate类(Dao)将对象进行增删查改
5.跳转页面
// 转发到注册结果页面
request.getRequestDispatcher("result.jsp").forward(request, response);