目录
1. 如何接受请求的参数值
1.1.什么是请求?
比如: form表单提交action请求路径。 比如: 超链接携带请求参数跳转到其他页面。
从一个地址跳转到另一个地址,再另一个地址获取请求的参数值。
<body>
<%--表单携带请求参数
action:表示表单提交的路径。
method:表示表单的提交方式。get和post
--%>
<form action="indexDo.jsp" method="post">
账号: <input type="text" name="uname"/><br>
密码: <input type="text" name="pwd"/><br>
<input type="submit" value="注册"/>
</form>
</body>
1.2.如何获取数据
如何从indexDo获取表单提交的数据
代码展示:
<%
//request属于jsp中的内置对象,无需自己创建该对象。
String name = request.getParameter("uname");//获取请求的参数值。uname必须和表单的输入的名称一致。
String pwd = request.getParameter("pwd"); //接受到表单提交的账号和密码了。
out.print("账号===="+name+"<br>");
out.print("密码===="+pwd+"<br>");
%>
注意: 如果是单选按钮和复选按钮必须设置属性value。 如果获取的是多个值,那么必须使用request.getParameterValues(""); 单个值直接使用request.getParameter("")
先把: jsp-api.jar和servlet-api.jar放入WEB-INF/lib文件夹下。
1.3. 接受超链接提交的数据
<%--超链接: 超链接如何传参 使用?key=value&key=value--%>
<a href="indexDo02.jsp?name=ykq&age=18">连接</a>
1.4. 乱码如何解决
接受参数的时候设置一下UTF-8的编码。
request.setCharacterEncoding("utf-8");
常见的编码:
ISO-8859-1: 国际编码 但是不支持中文。
UTF-8: 万能编码支持英文中文以及繁体中文。----企业开发都是用UTF-8
GBK: 中文编码.简体中文和繁体中文.
GB2312: 中文编码 但是它支持简体中文。
登录流程图:
2.jsp+dao整合
2.1为何要整合
我们上面在未整合时写登录,账号和密码的比对都是和固定值对比,实际我们应该和数据库中的账号和密码进行对比。
2.2.完善登录
登录的流程图:
2.3.注册
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>注册界面</title>
</head>
<body>
<form action="registDo.jsp" method="post">
用户名:<input type="text" name="username">
密码:<input type="text" name="userpassword">
<input type="submit" value="提交">
</form>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>注册界面操作</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
String userpassword = request.getParameter("userpassword");
UserDao userDao=new UserDao();
User user=new User();
user.setUername(username);
user.setPassword(userpassword);
userDao.add(user);
response.sendRedirect("main.jsp");
%>
</body>
</html>
2.4.查询所有的学生信息并展示到网页
(1)封装一个学生的实体类.
public class Student {
private int id;
private String stuName;//字段名stu_name 那么我们java属性尽量符合驼峰命名
private String address;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
(2) 封装一个学生的操作类。
//查询所有学生信息的方法
public List<Student> findAll(){
List<Student> list = new ArrayList<>();
try {
getConn();
String sql="select * from t_student";
ps = connection.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()){
Student student=new Student();
student.setId(rs.getInt("id"));
student.setStuName(rs.getString("stu_name"));
student.setAddress(rs.getString("address"));
student.setAge(rs.getInt("stu_age"));
list.add(student);
}
}catch (Exception e){
e.printStackTrace();
}finally {
closeAll();
}
return list;
}
(3)页面就调用学生的操作类中的查询所有的方法。
<%@ page import="com.ykq.dao.StudentDao" %>
<%@ page import="com.ykq.entity.Student" %>
<%@ page import="java.util.List" %><%--
Created by IntelliJ IDEA.
User: ykq
Date: 2022/5/10
Time: 15:35
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>
===============登录成功欢迎来到我的主场=========================
<table border="1" cellpadding="0" cellspacing="0" width="600" align="center">
<tr>
<th>编号</th>
<th>姓名</th>
<th>年龄</th>
<th>地址</th>
<th>操作</th>
</tr>
<%
StudentDao studentDao = new StudentDao();
List<Student> list = studentDao.findAll();
for (Student student : list) {
//插入html标签 out.print(); 这种拼接字符串麻烦
%>
<tr>
<td><%=student.getId()%></td>
<td><%=student.getStuName()%></td>
<td><%=student.getAge()%></td>
<td><%=student.getAddress()%></td>
<td>删除 修改</td>
</tr>
<%
}
%>
</table>
</body>
</html>
2.5.删除
deleteDo.jsp
<%@ page import="com.ykq.dao.StudentDao" %><%--
Created by IntelliJ IDEA.
User: ykq
Date: 2022/5/11
Time: 15:47
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>
<%
//接受传递的id值
String id = request.getParameter("id");
//调用studentDao中的删除方法把接受的id传递给该方法。
StudentDao studentDao=new StudentDao();
studentDao.delete(Integer.parseInt(id));//为什么报错- Integer.parseInt(id) 把数字字符串转换为整形
//跳转
response.sendRedirect("success.jsp");
%>
</body>
</html>
studentDao:
//根据id删除学员信息 测试该方法
public void delete(int id){
String sql="delete from t_student where id=?";
edit(sql,id);
}
2.6.修改
update.jsp
<%@ page import="com.ykq.dao.StudentDao" %>
<%@ page import="com.ykq.entity.Student" %><%--
Created by IntelliJ IDEA.
User: ykq
Date: 2022/5/12
Time: 14:14
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>修改界面</title>
</head>
<body>
<%
String id = request.getParameter("id");
//根据id查询该id对应的学员信息---Dao
StudentDao studentDao = new StudentDao();
Student student = studentDao.findById(Integer.parseInt(id));
//由于修改需要按照id进行修改 所以我们应该把id传递过去
%>
<form action="updateDo.jsp" method="post">
<%--隐藏域就是再浏览器上看不见该输入框,但是该隐藏域也属于表单元素。会随着表单的提交而被提交--%>
<input type="hidden" value="<%=student.getId()%>" name="id"/>
<%-- 编号:<input type="text" value="<%=student.getId()%>" name="id" readonly/><br>--%>
账号:<input type="text" value="<%=student.getStuName()%>" name="stuName"/><br>
年龄:<input type="text" value="<%=student.getAge()%>" name="age"/><br>
账号:<input type="text" value="<%=student.getAddress()%>" name="address"/><br>
<%--整个按钮是表单提交按钮 表单提交只会提交表单元素的数据。--%>
<input type="submit" value="确认修改"/>
<%--这个按钮是普通按钮它不会让表单提交走。
<input type="button" value="迪纳基" onclick="location.href='a.jsp?'"/>
--%>
</form>
</body>
</html>
dao
//根据id查询学员信息
public Student findById(int id){ //怀疑数据库中的类型为int. 可以使用string
Student student=null;
try {
getConn();//不行因为closeAll关闭的是父类中的资源
String sql = "select * from t_student where id=?";
ps = connection.prepareStatement(sql);
ps.setInt(1,id);
rs = ps.executeQuery();
while (rs.next()) {
student = new Student();
student.setId(rs.getInt("id"));
student.setStuName(rs.getString("stu_name"));
student.setAddress(rs.getString("address"));
student.setAge(rs.getInt("stu_age"));
}
}catch (Exception e){
e.printStackTrace();
}finally {
closeAll();
}
return student;
}
updateDo.jsp
<%@ page import="com.ykq.dao.StudentDao" %>
<%@ page import="com.ykq.entity.Student" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
//编码设置
request.setCharacterEncoding("utf-8");
//接受修改后的学员信息
String id = request.getParameter("id");
String name = request.getParameter("stuName");
String age = request.getParameter("age");
String address = request.getParameter("address");
// 输出你接受的数据---目标防止有错误。
//把接受的信息放入数据库。---dao.
StudentDao studentDao=new StudentDao();
Student s=new Student(Integer.parseInt(id),name,address,Integer.parseInt(age));
studentDao.update(s);
//跳转
response.sendRedirect("success.jsp");
%>
</body>
</html>
dao:
//修改学员信息---你必须测试该方法是否正确。
public void update(Student student){ //这里传递Student对象目的为后期框架做嫁衣
String sql = "update t_student set stu_name=?,stu_age=?,address=? where id=?"; edit(sql,student.getStuName(),student.getAge(),student.getAddress(),student.getId());
}
3.完善
3.1 .登录失败后应该有提示
登录失败时跳转到登录页面携带一个参数
UserDao userDao=new UserDao();
User user=new User();
User userAndPass = userDao.findUserAndPass(username, userpassword);
if(userAndPass!=null){//登录成功跳转到成功界面
response.sendRedirect("success.jsp");
}else{//跳转到登录页面error=1表示登录失败的标志
response.sendRedirect("main.jsp?error=1");
}
登录页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>主界面</title>
</head>
<body>
<%
//接受error参数,如果能接受到表示登录失败
String error = request.getParameter("error");
if(error!=null && "1".equals(error)){
out.print("<font color='red'>账号或密码错误</font>");
}
%>
<form method="post" action="login.jsp">
用户名:<input type="text" name="username">
密码:<input type="password" name="userpassword">
<input type="submit" value="登录">
<input type="button" value="注册" onclick="location.href='regist.jsp'">
</form>
</body>
</html>
3.2 .登录成功后保存登录的信息
把登录者的信息保存起来以供其他页面能够拿到当前登录者的信息。
把登录者的信息保存到哪里了? ----- session会话中。
什么是session会话? 浏览器与服务器进行会话的过程。当浏览器关闭那么表示会话结果。
session中保存的周期: 一次会话,如果会话结束那么保存到session中的数据也会丢失。
什么时候会话结束: (1)浏览器关闭 (2)服务器关闭。(3)超时 默认30分钟 如果30分钟浏览器没有刷新则超时,如果30分钟内刷新了则重新计时。
jsp中内置了一个session对象。
修改登录方法:
//根据账号和密码查询数据库
public User findByNameAndPwd(String name,String pwd){
User user=null;
try{
connection= getConn();
String sql="select * from t_user where username=? and password=?";
ps=connection.prepareStatement(sql);
ps.setObject(1,name);
ps.setObject(2,pwd);
rs = ps.executeQuery();
while (rs.next()){//进入while表明根据账号和密码查询出结果了。
user=new User();//实例化对象。
user.setUsername(rs.getString("username"));
user.setRealname(rs.getString("realname"));
user.setPassword(rs.getString("password"));
user.setId(rs.getInt("id"));
}
}catch (Exception e){
e.printStackTrace();
}finally {
closeAll();
}
return user;
}
session对象中常见的方法
1. setAttribute(key,value);key必须为String value是Object类型。
2. removeAttribute(key);移除session中指定的key
3. clear():清空session中所有的元素。
4. getAttribute(key);根据key获取session会话的数据
3.3. session会话的使用。---保存登录者的信息
如果没有登录 无法访问其他页面。
根据session中是否有用户 的信息,如果有表示登录成功,如果没有则表示没有登录
3.4.退出