servlet
Posted 初夏的一棵歪脖子树
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了servlet相关的知识,希望对你有一定的参考价值。
1 package com.zdsofe.servlet1; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 import javax.servlet.ServletConfig; 7 import javax.servlet.ServletException; 8 import javax.servlet.annotation.WebServlet; 9 import javax.servlet.http.HttpServlet; 10 import javax.servlet.http.HttpServletRequest; 11 import javax.servlet.http.HttpServletResponse; 12 13 /** 14 * Servlet implementation class HelloServlet1 15 */ 16 @WebServlet("/HelloServlet1") 17 public class HelloServlet1 extends HttpServlet { 18 private static final long serialVersionUID = 1L; 19 20 /** 21 * @see HttpServlet#HttpServlet() 22 */ 23 /* public HelloServlet1() { 24 super(); 25 }*/ 26 27 /** 28 * @see Servlet#init(ServletConfig) 29 */ 30 public void init(ServletConfig config) throws ServletException { 31 System.out.println("初始化成功"); 32 } 33 34 /** 35 * @see Servlet#destroy() 36 */ 37 public void destroy() { 38 System.out.println("关闭页面时的时候,销毁servlet---"); 39 } 40 41 /** 42 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 43 */ 44 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 45 response.getWriter().print("偷偷图"); 46 } 47 48 /** 49 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 50 */ 51 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 52 //设置请求字符编码 53 request.setCharacterEncoding("utf-8"); 54 //设置响应字符编码 55 response.setCharacterEncoding("utf-8"); 56 response.setContentType("text/html,charset=utf-8"); 57 //获取用户名 58 String userName=request.getParameter("userName"); 59 //密码 60 String password=request.getParameter("password"); 61 //获取客户端请求行中的uri 62 String uri=request.getRequestURI(); 63 System.out.println(uri); 64 //获取客户端请求行中的URL 65 StringBuffer url=request.getRequestURL(); 66 System.out.println(url); 67 //获取客户端IP 68 String ip=request.getRemoteAddr(); 69 System.out.println(ip); 70 //获取客户端完整主机名 71 String host=request.getRemoteHost(); 72 System.out.println(host); 73 //获取客户端端口号 74 int port=request.getRemotePort(); 75 System.out.println(port); 76 77 PrintWriter pWriter = response.getWriter(); 78 if("admin".equals(userName) && "123".equals(password)) 79 { 80 pWriter.print(""+userName + ",successful!!!"); 81 } 82 else 83 { 84 /*pWriter.print("failed!");*/ 85 //请求转发,请求的地址栏不发生变化 86 /* request.getRequestDispatcher("/Pages/lianxi.html").forward(request, response);*/ 87 88 //请求重定向,地址栏URL改变 89 90 response.sendRedirect("https://www.baidu.com"); 91 92 //在请求时添加参数 93 /*String reStr="哈哈哈"; 94 RequestDispatcher dispatcher=request.getRequestDispatcher("/Pages/01.jsp?a="+reStr+""); 95 dispatcher.forward(request, response);*/ 96 } 97 } 98 99 /** 100 * @see HttpServlet#doPut(HttpServletRequest, HttpServletResponse) 101 */ 102 protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 103 } 104 105 }
1 <%@page import="java.text.SimpleDateFormat"%> 2 <%@page import="java.util.Date"%> 3 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 4 pageEncoding="ISO-8859-1"%> 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 6 <html> 7 <head> 8 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 9 <title>Insert title here</title> 10 <% 11 String str=request.getParameter("a"); 12 out.print("<font color=‘red‘>"+str+"</font>"); 13 %> 14 </head> 15 <script type="text/javascript" src=../js/dateFormat.js></script> 16 <script> 17 var a=new Date(); 18 alert("时间"+a.Format("yyyy-MM-dd hh:mm:ss")); 19 </script> 20 <% 21 Date a=new Date(); 22 SimpleDateFormat s=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); 23 out.print(s.format(a)); 24 %> 25 <body> 26 27 </body> 28 </html>
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Insert title here</title> 6 </head> 7 <body> 8 <form action="../HelloServlet1" method="post"> 9 用户名:<input type="text" name="userName"/><br/> 10 密码:<input type="password" name="password"/><br/> 11 <button type="submit">提交</button> 12 </form> 13 </body> 14 </html>
//请求重定向,地址栏URL改变
String reStr="wer";
response.sendRedirect("/webProject1/Pages/01.jsp?a="+reStr+"");//路径必须要加工程名!
1、servlet的创建:
(1)如何在动态web项目中配置servlet请求: 两种方式:
a。在web.xml中配置: <servlet> <servlet-name>主题servlet名称</servlet-name> <servlet-class>com.ser.servlet类名/</servlet-class> </servlet> <servlet-mapping> <servlet-name>主题servlet名称</servlet-name> <url-pattren>访问的servlet的url(/serv1)</url-pattren> </servlet-mapping>
b。@WebServlet({"/serv1","/serv2"}) : servlet3.0版本支持的注解(一般在web。xml中顶部:版本信息:3.0)
(注意:上述的两种创建servlet的方式: web.xml中的url地址不能和注解后配置的地址一样,否则服务无法启动)
2、servlet的请求对象和响应对象 HttpServletRequest res:请求对象 res.getParameter("表单控件的name属性的值/url地址后面查询字符") res.setCharactEncoding("UTF-8"); res.getParameterValues("多选框或者单选框的name") res.getContextPath() :获取上下文路径 res.getHeaderName();:获取所有请求头部的名称:返回的是Enumeration<String> res.getRealPath()/ res.getServletPath/ ....
//request的请求转发对象: res.getRequestDispatcher("页面路径").forward(res,resp);
HttpServletResponse :响应对象 PrintWriter out = resp.getWriter(); out.Print("打印的内容(html内容)"); out.flush();刷新 out.close(); 关闭输出流(打印的信息不会被清除) resp.setCharactEncoding("编码格式");:防止响应页面的中文出现乱码 resp.setContextType("text/html charset="utf-8""); 重定向页面:resp.sendRedirect("/工程名/页面路径")
以上是关于servlet的主要内容,如果未能解决你的问题,请参考以下文章