jsp笔记

掌握使用request对象和response对象处理用户的请求和相应 熟练掌握JSP的转发和重定向

学习web项目的时候大家一定要会debug模式一定要过关,不会debug就不会去调试你的程序,检测不了你代码的运

404错误表示找不到路劲:url写错了,你的页面放到了web-inf

1、jsp的内置对象

jsp的内置对象:jsp本身自带的对象不需要进行new就直接可以使用的对象

jsp有9个内置对象,我们要掌握的是常用的6个,内置对象常用放发,内置对象的作用域

1、out对象

jsp页面中的输出对象他和 我们java中的System.out.println()是一个意思

<%= %>

out对象用于向浏览器输出数据 其常用的方法是print(), 该方法用于在页面中显示字符串信息

2、request对象

request 主要用于处理客户端请求

需求1、: 实现用户信息注册

用户注册的思路:获取页面信息,将信息保存到我们的数据库中(数据添加的过程)

1、编写用户注册页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户注册</title>
</head>
<body>
    <form name="form1" method="post" action="reginfo.jsp">
        <table border="0" align="center">
           <tr> 
                <td>用户名</td>
                <td> <input type="text" name="name"></td>
           </tr>
           <tr>
                <td>密码</td>
                <td ><input type="password" name="pwd"></td>
           </tr>
           <tr> 
                <td>信息来源</td>
                <td> 
                    <input type="checkbox" name="channel" value="报刊">报刊 
                    <input type="checkbox" name="channel" value="网络">网络 
                    <input type="checkbox" name="channel" value="朋友推荐"> 朋友推荐 
                    <input type="checkbox" name="channel" value="电视"> 电视
                 </td>
            </tr>
            <tr>
                <td colspan="2">
                    <input type="submit" value="提交"/>                   
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

2、注册成功页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册成功页面</title>
</head>
<body>
    <!--  拿去注册信息-->
    <%
        //设置字符编码
        request.setCharacterEncoding("utf-8");
        //获取用户名
        String sname = request.getParameter("name");
        String spwd = request.getParameter("pwd");
        //请求中获取表单中多个值
        String [] schannels = request.getParameterValues("channel");
    %>
    <div align="center">
        <table>
            <tr>
                <td>用户名:</td>
                <td><%=sname %></td>
            </tr>
            <tr>
                <td>密码:</td>
                <td><%=spwd %></td>
            </tr>
            <tr>
                <td>信息来源:</td>
                <td>
                     <%
                     if(schannels !=null){
                         for(String schannel:schannels){
                             out.print(schannel);
                         } 
                     }
                     %>
                </td>
            </tr>
        </table>
    </div>
    
</body>
</html>

需求2、实现用户登录功能

实现思路:用户登录页面,拿取页面信息到你的数据库里面去查询,有结果跳转到欢迎页,没有结果跳转到错误页面给出提示信息

1、用户登录页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户登录页面</title>
</head>
<body>
  <form name="form1" method="post" action="control.jsp">
    用户名:<input type="text" name="userName"> 
     密码:<input type="password" name="pwd"> 
        <input type="submit" value="登录"> 
  <form>
</body>
</html>

2、用户信息获取页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- 信息处理 获取用户名和密码进行比对   假如:用户名为sa 密码为sa  
    就跳转到欢迎页面,不是跳转到错误页面进行错误提示 -->
    
    <%
        request.setCharacterEncoding("UTF-8");
        //获取用户名
        String username = request.getParameter("userName");
      //获取密码
        String password = request.getParameter("pwd");
        //案例说我们应该拿着用户名和密码去你的数据库中查询是否存在
        //如果你的用户名是 sa 我们就执行登录成功跳转到 一个欢迎页面
        if(username.equals("李通")){
            //登录成功跳转到欢迎页面
            request.getRequestDispatcher("welcome.jsp").forward(request, response);
        }else{
            //登录失败跳转到错误页面
            request.getRequestDispatcher("erroy.jsp").forward(request, response);
        }
        
    %>
    
    
    
</body>
</html>

3、欢迎页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <%
    String username = request.getParameter("userName");
    %>
    欢迎<%=username %>来到本页面
</body>
</html>

4、错误页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
 登录失败
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值