day02 Servlet

1 Servlet使用方法

package com.yuxhu.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/* 1.创建一个类去继承HttpServlet
 * 2.重写doGet/doPost头方法
 *
 * HttpServletRequest  : 用来接受数据 (读)
 * HttpServletResponse : 用来响应数据 (写)
 * 
 * 直接运行是不行的,需要配置访问路径
 * 1.注解
 * 2.web.xml文件配置
 * 3.启动服务器之后页面怎么访问?
 *   localhost:8080/项目/访问路径(唯一)
 * 
 */
// @WebServlet(value="/yxh") //由于设置了servlet类,浏览器默认指向这里,所以不需要注解
public class ServletXml extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收请求 HttpServletRequest
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String user = new String(username.getBytes("iso-8859-1"),"utf-8");//解决乱码
        String pwd = new String(password.getBytes("iso-8859-1"),"utf-8");//解决乱码
                
        //响应结果 HttpServletResponse
        resp.setContentType("text/html;charset=utf-8"); //解决乱码
        PrintWriter pw = resp.getWriter();
        pw.write("账号:" + user + "
"); pw.write("密码:" + pwd); //控制台 System.out.println("账号:" + user); System.out.println("密码:" + pwd); } }

2 设置 servlet 类 web.xml



  Servlet1
  
    login.jsp
  
  
  
  
    
    xml
    
    com.ithc.demo.ServletXml
  
  
  
    
    xml
    
    /xml
  


3 浏览器页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here


    
//form表单
     


4 单选,多选,超链接

表单

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here



    
小米
华为
苹果
点击

servlet

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here



    
小米
华为
苹果
点击

你可能感兴趣的:(day02 Servlet)