标签:
Servlet的生命周期方法:
<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="提交">
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>
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>
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 } }
<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 } }
标签:
原文地址:http://www.cnblogs.com/boycelee/p/5444813.html