简单的前台传递后台的列子

首先需要导入jar包,导入的jar包如下图:
在web——>web-INF 点击右击new 点击Directory 名字名为lib——>lib赋值进入如下jar包后一定右击 Add as Directory as. 加入所创建的项目道中
在这里插入图片描述
index.jsp
<%–
Created by IntelliJ IDEA.
User: love
Date: 2020/2/28
Time: 9:18
To change this template use File | Settings | File Templates.
–%>

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
  <head>
<title>$Title$</title>
 </head>
 <body>
<%--   form 是表单 有from才可以实现提交行为
  action 传递到那里去
  method 提交的方法
                   |---  get  :  【默认】
	1.会将你的用户和密码(或者是别的数据),裸露在浏览器的地址中,
	2.(前台提交)后台提交的非常快
	3. 容量有限
                        |---  post:
	1.会将你的用户和密码(或者是背的数据)没有体现在浏览器的地址中(安全)
	2.提交没有get快
	3.理论上时无限的--%>
	 <form action="/userServlet" method="post">
	 <%--    table :表格  border : 边 1 px   width :宽 400 px --%>
	    <table border="1px" width="400px">
	    <%--      <tr> 定义表的行  <td>定义表的单元--%>
  <tr>
    <td>用户名:</td>
    <td>
    <%--      input :输入 type:类型 =“文本档” 名字--%>
	  <input type="text" name="username">
    </td>
  </tr>
  <tr>
    <td>密码:</td>
    <td>
      <input type="text" name="password">
    </td>
  </tr>
  <tr>
  <%--        colspan  合并列
         rowspan  合并行   
          Value    值--%>

    <td colspan="2">
      <input type="submit" value="登录">
    </td>
  </tr>
</table>
 </form>
   </body>
   </html>

success.jsp

	<%--
Created by IntelliJ IDEA.
User: love
Date: 2020/3/1
Time: 14:34
To change this template use File | Settings | 				File Templates.
--%>
<%@ page 		
contentType="text/html;charset=UTF-8" language="java" %>

<html>
<head>
	<title>成功</title>
</head>
<body>
<h2>我已接受到了你的用户名称;${user.username}</h2>
</body>
</html>

web——web-INF——web.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>
    3.<servlet-name>UserServlet</servlet-name>
    4.        <servlet-class>cn.javabs.houtai.UserServlet</servlet-class>
</servlet>
    <servlet-mapping>
       2.  <servlet-name>UserServlet</servlet-name>
    1.   <url-pattern>/userServlet</url-pattern>
     </servlet-mapping>
     </web-app>

UserServlet.java

package cn.javas.houtai;

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("/userServlet")
public class UserServlet extends HttpServlet {
UserDao userDao = new UserDao();
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取前台数据
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    User user = userDao.login(username,password);
    request.setAttribute("user",user);
    request.getRequestDispatcher("/success.jsp").forward(request,response);
}

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

}

}

UserDao.java

package cn.javas.houtai;
mport com.mysql.jdbc.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class UserDao {
Connection conn = (Connection) JdbcUtil.getConnection();
public User login(String username, String password) {

    try {
        String sql = "select *from user where username = ? and possword = ?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1, username);
        ps.setString(2, password);
        System.out.println("ps:" + ps);

        ResultSet resultSet = ps.executeQuery();
        User user = new User();
        while (resultSet.next()) {
            int id = resultSet.getInt("id");
            String name = resultSet.getString("username");
            String pwd = resultSet.getString("password");
            String sex = resultSet.getString("sex");
            String email = resultSet.getString("email");
            String role = resultSet.getString("role");

            user.setId(id);
            user.getName(name);
            user.getPassword(password);
            user.getSex(sex);
            user.getEmail(email);
            user.getRole(role);

        }
        return user;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

}

}
JdbcUtils.java
package cn.javas.houtai;

import java.sql.Connection;
import java.sql.DriverManager;


public class JdbcUtil {
public static Connection getConnection() {
    String driver = "com.mysql.jdbc.Driver";
    String url = "jdbc:mysql:localhost:3306/day28"; // jdbc:mysql://127.0.01:3306/day28 | jdbc:mysql:///day28
    String username = "root";
    String password = "root";
    try {
        Class.forName(driver);
        Connection connection = DriverManager.getConnection(url, username, password);
    } catch (Exception e) {
        throw new RuntimeException(e);


    }
return null;
}

}
User.java

package cn.javas.houtai;
public class User {
// 私有化 数据类型 变量名称
private int id;
private String name;
private String password;
private String sex;
private String email;
// role 角色
private String role;
// 快捷键 alt+insert 生成getter和setting方法
// tostring方法等方法

public User() {
    super();
}

@Override
public int hashCode() {
    return super.hashCode();
}

public User(String name, String password, String sex, String email, String role) {
    this.name = name;
    this.password = password;
    this.sex = sex;
    this.email = email;
    this.role = role;
}

@Override
public String toString() {
    return "User{" +
            "id=" + id +
            ", name='" + name + '\'' +
            ", password='" + password + '\'' +
            ", sex='" + sex + '\'' +
            ", email='" + email + '\'' +
            ", role='" + role + '\'' +
            '}';
}

public int getId() {
    return id;
}

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

public String getName(String name) {
    return this.name;
}

public void setName(String name) {
    this.name = name;
}

public String getPassword(String password) {
    return this.password;
}

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

public String getSex(String sex) {
    return this.sex;
}

public void setSex(String sex) {
    this.sex = sex;
}

public String getEmail(String email) {
    return this.email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getRole(String role) {
    return this.role;
}

public void setRole(String role) {
    this.role = role;
    }
}

General :通用
Request URL:请求路径
http//locathost:8888/day28/userSerlet
本地主机:端口号/ 项目名称/访问路径
Request method: post 请求的方式
status code:200 可以通行
Response Headers :响应头
Request Headers:请求头
Form Headers :表单数据
username:wangcai
password:admin123

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值