Javaweb三大组件之一 servlet
1.servlet基本使用
注意:单例:init只会执行一次
线程不安全
1.1 创建方式
(1)实现servlet接口【不使用】
package cn.getword.servlet; import javax.servlet.*; import java.io.IOException; public class LoginServlet implements Servlet { @Override public void init(ServletConfig servletConfig) throws ServletException { System.out.println("init servlet..."); } @Override public ServletConfig getServletConfig() { return null; } @Override public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { servletResponse.getWriter().print("hello servlet"); } @Override public String getServletInfo() { return null; } @Override public void destroy() { } }
init方法用来配置servlet初始化参数:
第一次访问servlet时,进行servlet实例化,执行init方法。
需要服务器启动时创建servlet实例:在web.xml中配置:load-on-startup :非负整数
<servlet> <servlet-name>login</servlet-name> <servlet-class>cn.getword.servlet.LoginServlet</servlet-class> <init-param> <param-name>charset</param-name> <param-value>utf-8</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>login</servlet-name> <url-pattern>/login</url-pattern> </servlet-mapping>
url-pattern的写法:*.do , /servlet/* 【通配符 * 要么放在最前面,要么放在最后面】
获取参数信息:
private ServletConfig config; //将配置对象保存下来 @Override public void init(ServletConfig servletConfig) throws ServletException { System.out.println("init servlet..."); this.config = servletConfig; servletConfig.getInitParameter("charset"); }
服务终止前执行destroy方法,用于关闭非内存资源。
(2)继承GenericServlet类【不使用】
package cn.getword.servlet; import javax.servlet.GenericServlet; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import java.io.IOException; public class Login2Servlet extends GenericServlet { @Override public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { } }
(3) 继承HTTPServlet类【使用】 注解或配置文件
package cn.getword.servlet; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet(name = "Login3Servlet", urlPatterns = {"/login3"}) public class Login3Servlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); response.getWriter().print("你好,servlet"); } }
通过request获取session对象:
Session session = request.getSession();
获取参数信息:
@WebServlet("/register") public class RegisterServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); String username = request.getParameter("username"); String phone = request.getParameter("username"); String pwd = request.getParameter("username"); String[] hobby = request.getParameterValues("hobby");//获取复选框值 String gender = request.getParameter("gender"); String course = request.getParameter("course"); response.getWriter().print("hello"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }
2. 解决乱码
String name = request.getParameter("name");
方法一:转码
name = new String(name.getBytes("ISO-8859-1"));
方法二:【推荐】
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String username = request.getParameter("username");
3. 转发
request.getRequestDispatcher("").forward(request, response);
注意:不可以跨域
4.重定向
response.sendRedirect("");
注意:可以跨域
5.cookie【服务器端操作】
添加cookie:【使用response向客户端写入cookie】
response.addCookie(new Cookie("userid", "10"));
获取cookie:【使用request获取cookie】
request.getCookies()
6.session
(1)使用方法
session.setAttribute("userid", "1"); session.getAttribute("userid");
session.removeAttribute(key);
注意:作用范围为回话范围。
(2)强制使得session失效,一般用于用户注销
session.invalidate();
7. pageContext
获取其他内置对象:
pageContext.getRequest();
pageContext.getResponse();
pageContext.getServletConfig();
8.application对象
在web.xml中配置web项目的全局初始化参数:
<context-param> <param-name>jdbc</param-name> <param-value></param-value> </context-param>
使用application对象获取参数信息:
<% Enumeration<String> initParameterNames = application.getInitParameterNames(); while(initParameterNames.hasMoreElements()){ String key = initParameterNames.nextElement(); out.print(key+":"+application.getInitParameter(key)+"<br />"); } %>
end