Web基础和servlet基础

TomCat的目录结构

Bin:脚本目录(存放启动、关闭这些命令)
Conf:存放配置文件的目录
Lib:存放jar包
Logs: 存放日志文件
Temp: 临时文件
Webapps:  项目发布目录
Work:  jsp文件编译后生成的.java文件和.class文件存放的目录

请求行,请求头和请求体

请求行:
        请求方法  请求URL   HTTP版本
请求头:
        Host:            
        User-Agent:  
        Accept:
        Accept-Language
        Accept-Encoding:
        Referer:
        onnection: 
        Cookie:       
请求体:
        get没有请求体,即跟在URL后面,post有请求体,不展示在地址栏里                  

响应行,响应头和响应体

 1 响应行:
 2         HTTP协议  状态码
 3 响应头:
 4         Accept-Ranges: bytes
 5         Cache-Control: max-age=0
 6         Connection: keep-alive
 7         Content-Length: 114
 8         Content-Type: text/html
 9         Date: Sun, 01 Sep 2019 14:09:54 GMT
10         Etag: "5d2c473c-72"
11         Expires: Sun, 01 Sep 2019 14:09:54 GMT
12         Last-Modified: Mon, 15 Jul 2019 09:28:28 GMT
13         Server: nginx
14 请求体:
15         图片,文本,html ......       

Get请求乱码和Post请求乱码和响应乱码

get乱码
        username = new String(username.getBytes("ISO-8895-1"),"utf-8")
    
        设置xml URIEncoding"UTF-8";
Post乱码
        request.setCharacterEncoding("UTF-8");
response.setContextType("text/html;charset=utf-8");

servlet 入门和执行流程

问题:为什么在浏览器输入http://localhost:8080/day10/demo1就能够执行doGet()方法

    在服务器下寻找day10项目下的xml文件
    在xml下寻找/demo1
    根据/demo1找到<servlet-name>
    再次找相同名字的<servlet-name>
    对应一个完整的类名
    使用反射加载类名

案例: 使用servlet完成用户登录

   登录页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="Setvlet" method="post">
    username:<input type="text" name="username" ><br>
    password:<input type="password" name="password" ><br>
    验证码:<input type="text" name="CheckCode"><img src="CheckCodeSetvlet" id="CheckCode">
    <a id="CheckCode" >看不清,换一张</a>
    <input type="submit" value=" login ">
</form>
</body>
</html>

 

  调用servlet

@WebServlet(name = "Servlet",urlPatterns = "/Setvlet")
public class Servlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        User user = new User();
        user.setUsername(username);
        user.setPassword(password);

        UserService userService = new UserServiceImpl();
        User user1 = userService.login(user);

        response.setContentType("text/html;charset=utf-8");

        ServletContext sc = this.getServletContext();

        if(user!=null){
            int num = 0;
            if(sc.getAttribute("num")!=null){
                num = (int)sc.getAttribute("num");
            }
            sc.setAttribute("num", ++num);
            PrintWriter out = response.getWriter();
            num = (int)sc.getAttribute("num");
            out.print("<h1>第"+num+"位登录成功的用户</h1>");
        }else{
            response.sendRedirect("login.html");
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}

 

  调用service层

public class UserServiceImpl implements UserService {
    @Override
    public User login(User user) {
        UserDao userDao = new UserDaoimpl();
        User user1 = userDao.login(user);
        return user1;
    }

 

  调用dao层

public class UserDaoimpl implements UserDao {
    @Override
    public User login(User user) {
        QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
        String sql = "select * from user where username = ? and password = ?";
        User user1 = null;
        try {
            user1 = qr.query(sql, new BeanHandler<>(User.class),new Object[]{user.getPassword(),user.getPassword()});
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user1;
    }
}

  bean层

public class User {
    private int id;
    private String username;
    private String password;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    private int age;
}

 

  utils层  C3P0连接池

import java.sql.Connection;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class C3P0Utils {
    private static DataSource ds;
    static{
        ds = new ComboPooledDataSource();
    }
    public static DataSource getDataSource(){
        return ds;
    }
    public static Connection getConnection(){
        Connection conn = null;
        try{
            conn = ds.getConnection();
        }catch(Exception e){
            e.printStackTrace();
        }
        return conn;
    }
}

 

  c3p0-config.xml默认配置

     <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql:///0826</property>
        <property name="user">root</property>
        <property name="password">123</property>

案例登录流程分析

转载于:https://www.cnblogs.com/lovemeng1314/p/11444191.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值