javaWeb 每天积极向上

Servlet三大核心组件: Servlet , Filter , Listener

1.Servlet
作用:
用来处理客户端发送过来的请求,并对改请求作出响应
1. 获取接收客户端发送过来的请求数据
2. 处理请求
3. 将处理的结果通过响应发送到客户端

(1).编写第一个Servlet程序
方式一 .XML映射:

/**
 * 1.继承HttpServlet
 * 2.重写doGet和doPost函数
 * 3.在web.xml里编写servlet映射
 */
public class HelloServlet extends HttpServlet {
     
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response){
     
        System.out.println("Hello Servlet Get请求");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response){
     
        System.out.println("Hello Servlet Post请求");
    }
}

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <servlet>
        <servlet-name>helloServletservlet-name>
        <servlet-class>com.zx.servlet.HelloServletservlet-class>
    servlet>

    <servlet-mapping>
        <servlet-name>helloServletservlet-name>
        <url-pattern>/hellourl-pattern> 
    servlet-mapping>
web-app>

方式二.注解映射:

/**
 * 注解名称
 * 注解映射
 */
@WebServlet(name="HelloServlet",urlPatterns="/hello")
public class HelloServlet extends HttpServlet {
     
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response){
     
        System.out.println("Hello Servlet");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response){
     
        System.out.println("Hello Servlet");
    }
}

(2).请求(HttpServletRequest/Response)
1.获取请求行和请求头

@Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response){
     
    	/** 获取请求行 */
        request.getMethod();//返回请求方法 get/post/head/delete/put
        request.getRequestURL();//返回URL
        request.getRemoteAddr();//返回客户端IP地址
        request.getRemotePort();//返回客户端端口号
        request.getLocalAddr();//获取服务器当前接收请求的IP地址
        request.getContextPath();//获取URL中属于web应用程序的路径
        request.getProtocol();//获取请求行中的协议名和版本
        
        /** 获取请求头 */
        Enumeration<String> headName =request.getHeaderNames();
        while(headName.hasMoreElements()){
     
            System.out.println(request.getHeader(headName.nextElement()));
        }
    }

2.请求 (HttpServletRequest)
获取请求参数

@Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response){
     
        //返回字符类型
        String context_string=request.getParameter("name");
        //返回枚举类型
        Enumeration<String> context_enumeration=request.getParameterNames();
        //返回map类型
        Map<String,String[]> context_map=request.getParameterMap();
    }

3.响应 (HttpServletResponse)
发送响应行和响应头

@Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
    throws IOException{
     
        //设置状态码
        response.setStatus(200);
        //改方法标识发送错误信息的状态码,例如404找不到资源
        response.sendError(404);
        response.sendError(404,"找不到请求的资源");
        //设置Servlet输出内容的类型 img/jpeg 或者是 text/html;charset=UTF-8 
        response.setContentType("text/html;charset=UTF-8");
    }

4.字节流和字符流

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException{
     
      String message="登陆成功";
      
      //字节流 适用于输出媒体文件
      ServletOutputStream out=response.getOutputStream();
      out.write(message.getBytes());
        
      //字符流 适用于输出网页文档
      response.setContentType("text/html;charset=UTF-8"); //解决乱码问题
      PrintWriter writer=response.getWriter();
      writer.write(message);
   }

小案例-验证码切换

@Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException{
     
        int width=100;
        int height=30;
        String checkCode="ABCDEFGH12345";
        Random random=new Random();
        //创建一张图片
        BufferedImage img=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
        //获取img的画板
        Graphics graphics= img.getGraphics();
        graphics.setColor(Color.gray);
        graphics.fillRect(0,0,width,height);
        //设置验证码
        graphics.setColor(Color.black);
        for(int i=0;i<5;i++){
     
            int randomParam=random.nextInt(checkCode.length());
            String randomStr=checkCode.substring(randomParam,randomParam+1);
            graphics.drawString(randomStr,i*22,18);
        }
        ImageIO.write(img,"jpg",response.getOutputStream());
    }

你可能感兴趣的:(笔记,java)