zoukankan      html  css  js  c++  java
  • 使用Eclipse开发Web项目(JSP)——简单登录、无sql

    1.使用Eclipse开发Web项目(JSP) tomcat

    2.在Eclipse中创建的Web项目:

    浏览器可以直接访问webContent中的文件

    例如http://localhost:8080/MyJspProject/index1.jsp

    其中的index1.jsp就在WebContent目录中;

    但是WEB-INF中的文件 无法通过客户端(浏览器)直接访问,只能通过请求转发来访问

    注意:并不是任何的内部跳转都能访问WEB-INF;原因是跳转有两种方式:请求转发、重定向

    3.配置tomcat运行时环境

    jsp <->Servlet

    a.将tomcat/lib中的servlet-api.jar加入项目的构建路径(只加一个)

    b.右键项目 -> Build Path -> Add library - Server Runtime(加一堆jar)【推荐】

    4.部署tomcat

    在servers面板新建一个tomcat实例,再在该实例中部署项目(右键-add)

    注意:一般建议将eclipse中的tomcat与本地tomcat保持一致;

    将eclipse中的tomcat设置为托管模式:【第一次】创建tomcat实例之后,双击,选择Server Location的第二个

    5.统一字符集编码

    a.编码分类:

    设计jsp文件的编码(jsp文件中的pageEncodeing属性):jsp -> java

    设置浏览器读取jsp文件的编码(jsp文件中content属性)

    一般将上述设置成一致的编码,推荐使用UTF-8

    b.文本编码:

    i.将整个Eclipse中的文件统一设置(以后的jsp编码都会utf-8)【推荐】

    ii.设置某一项目(右键文件-properties)

    iii.设置单独文件

    6.JSP的页面元素

    HTML java代码(脚本Scriptlet) 指令 注释

    a.脚本Scriptlet

    i.

    1 <%
    2     局部变量、java语句
    3 %>

    ii.

    1 <%!
    2     全局变量、定义方法
    3 %>

    iii.

    1 <%=
    2     输出表达式
    3 %>

    修改web.xml、配置文件、java需要重启tomcat服务,但是如果修改Jsp/html/js/css代码不需要重启

    注意:out.println()不能回车;要想回车:<br>,即out.print() <%= %>可以直接解析html代码

    b.指令

    page指令

    1 <%@ page language="java" contentType="text/html; charset=UTF-8"
    2     pageEncoding="UTF-8" import="java.util.Date"%>

    属性:

    language:jsp页面使用的脚本语言

    import:导入类

    pageEnconding:jsp文件自身编码 jsp -> java

    contentType:浏览器解析自身的编码

    c.注释

    html注释

    1 <!--可以被客户通过浏览器查看源码所观察到-->

    java注释

    1 //
    2 /*...*/

    jsp注释

    1 <%--    --%>

    7.JSP九大内置对象

    (自带,无需new也能使用的对象)

    out:输出对象,向客户端输出内容

    request:请求对象;存储“客户端向服务端发送的请求消息“

    request对象的常见方法:

    String getParameter(String name); 根据请求的字段名key(input标签的name属性),返回字段值value(input标签的value属性)

    String[] getParameterValues(String name); 根据请求的字段名key,返回多个字段值value(checkbox)

    void setCharacterEncoding("编码格式utf-8"); 设置post请求编码(tomcat7以前默认iso-8859-1,tomcat8以后改成了utf-8)

    getRequestDispartcher("b.jsp").forward(request,response); 请求转发的方式跳转页面 A -> B

    ServletContext getServerContext(); 获取项目的ServletContext对象

    示例:注册

    register.jsp
     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <title>Insert title here</title>
     8 </head>
     9 <body>
    10     <form action="show.jsp">
    11         用户名:<input type="text" name="uname"/><br/>
    12&nbsp;&nbsp;&nbsp;码:<input type="password" name="upwd"/><br/>
    13&nbsp;&nbsp;&nbsp;龄:<input type="text" name="uage"/><br/>
    14&nbsp;&nbsp;&nbsp;好:<br/>
    15         <input type="checkbox" name="uhobbies" value="足球"/>足球
    16         <input type="checkbox" name="uhobbies" value="篮球"/>篮球
    17         <input type="checkbox" name="uhobbies" value="乒乓球"/>乒乓球<br/>
    18         <input type="submit" value="注册">
    19     </form>
    20 </body>
    21 </html>
    show.jsp
     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <title>Insert title here</title>
     8 </head>
     9 <body>
    10     <%
    11         //设置编码
    12         request.setCharacterEncoding("utf-8");
    13         String name = request.getParameter("uname");
    14         int age = Integer.parseInt(request.getParameter("uage"));
    15         String pwd = request.getParameter("upwd");
    16         
    17         String[] hobbies = request.getParameterValues("uhobbies");
    18     %>
    19     注册成功,信息如下:<br/>
    20     姓名:<%=name %><br/>
    21     年龄:<%=age %><br/>
    22     密码:<%=pwd %><br/>
    23     <%
    24         if(hobbies !=null){     //控制没有爱好则不显示
    25             out.print("爱好:");
    26             for(String hobby:hobbies){
    27                 out.print(hobby + "&nbsp;");
    28             }
    29         }
    30     %>
    31 </body>
    32 </html>

    http://localhost:8080/MyJspProject/show.jsp?uname=zs&upwd=abc&uage=22&uhobbies=%E7%AF%AE%E7%90%83&uhobbies=%E4%B9%92%E4%B9%93%E7%90%83

    连接/文件?参数名1=参数值1&参数名2=参数值2&参数名3=参数值3

    get提交方式:method="get"和地址栏、超链接(<a href="xx">)请求方式默认都属于get提交方式

    get与post请求方式的区别:

    a.get方式在地址栏显示请求信息(但是地址栏能够容纳的信息有限,4-5KB;如果请求数据存在大文件)

    b.文件上传操作,必须是post【推荐】

    response:响应对象

    提供的方法:

    void addCookie(Cookie cookie); 服务端向客户端增加cookie对象

    void sendRedirect(String location) throws IOException; 页面跳转的一种方式(重定向)

    void setContentType(String type);设置服务端响应的代码(设置服务端的contentType类型)

    示例:登录

    login.jsp -> check.jsp -> success.jsp

    login.jsp
     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <title>Insert title here</title>
     8 </head>
     9 <body>
    10     <form action="check.jsp" method="post">
    11         用户名:<input type="text" name="uname"><br/>
    12         密码:<input type="password" name="upwd"><br/>
    13         <input type="submit" value="登录"><br/>
    14     </form>
    15 </body>
    16 </html>
    check.jsp
     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <title>Insert title here</title>
     8 </head>
     9 <body>
    10     <%
    11         request.setCharacterEncoding("utf-8");
    12         String name = request.getParameter("uname");
    13         String pwd = request.getParameter("upwd");
    14         if(name.equals("zs")&&pwd.equals("abc")){
    15             //通过重定向跳转,结果导致数据丢失
    16             //response.sendRedirect("success.jsp");
    17             //请求转发跳转:可以获取到数据,并且地址栏没有改变(仍然保留转发时的页面)
    18             request.getRequestDispatcher("success.jsp").forward(request, response);
    19         }else{
    20             //登录失败
    21             out.print("用户名或密码错误!");
    22         }
    23     %>
    24 </body>
    25 </html>
    success.jsp
     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <title>Insert title here</title>
     8 </head>
     9 <body>
    10     登陆成功!<br/>
    11     欢迎您:
    12     <%
    13         String name = request.getParameter("uname");
    14         out.print(name);
    15     %>
    16 </body>
    17 </html>

    请求转发和重定向的区别

     请求转发重定向
    地址栏是否改变 不变(check.jsp) 改变(success.jsp)
    是否保留第一次请求时的数据 保留 不保留 --4种范围对象
    请求的次数 1 2
    跳转发生的位置 服务端 客户端发起的第二次跳转

    转发、重定向:

    转发:张三(客户端) -> 【服务窗口A(服务器) -> 服务窗口B】

    重定向:张三(客户端) -> 服务窗口A(服务端) -> 去找B

    张三(客户端) ->服务窗口B(服务端) -> 结束

    session(服务端,内置对象)

    Cookie(客户端,不是内置对象):

    Cookie是由服务端生成的,再发给客户端保存

    相当于本地缓存的作用:客户端(hello.jsp)->服务端(hello.mp4;zs/abc)

    作用:提高访问服务器的效率,但是安全性较差。

    Cookie:key=value

    javax.servlet.http.Cookie

    public Cookie(String name,String value)

    String getName() 获取name

    String getValue() 获取value

    void setMaxAge(int expiry); 最大有效期(s)

    服务器准备Cookie:

    response.addCookie(Cookie cookie)

    页面跳转(转发、重定向)

    客户端获取Cookie:request.getCookies();

    a.服务端增加Cookie:response对象;客户端获取对象:request对象

    b.不能直接获取某一个单独对象,只能一次性将全部的Cookie拿到

    通过F12可以发现,除了自己设置的Cookie对象外,还有一个name为JSESSIONID的cookie

    建议cookie中只保存英文、数字,否则需要进行编码、解码处理

    使用Cookie实现记住用户名操作

    login.jsp
     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <title>Insert title here</title>
     8 </head>
     9 <body>
    10     <%!
    11         String uname;
    12     %>
    13     <%
    14         Cookie[] cookies = request.getCookies();
    15         for(Cookie cookie:cookies){
    16             if(cookie.getName().equals("uname")){
    17                 uname = cookie.getValue();
    18             }
    19         }
    20     %>
    21     <form action="check.jsp" method="post">
    22         用户名:<input type="text" name="uname" value="<%=(uname==null?"":uname)%>"><br/>
    23         密码:<input type="password" name="upwd"><br/>
    24         <input type="submit" value="登录"><br/>
    25     </form>
    26 </body>
    27 </html>
    check.jsp
     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <title>Insert title here</title>
     8 </head>
     9 <body>
    10     <%
    11         request.setCharacterEncoding("utf-8");
    12         String name = request.getParameter("uname");
    13         String pwd = request.getParameter("upwd");
    14         
    15         //将用户名加入到Cookie中
    16         //Cookie cookie = new Cookie("key","value");
    17         Cookie cookie = new Cookie("uname",name);
    18         response.addCookie(cookie);
    19         
    20         response.sendRedirect("result.jsp");
    21         
    22         /* if(name.equals("zs")&&pwd.equals("abc")){
    23             //通过重定向跳转,结果导致数据丢失
    24             //response.sendRedirect("success.jsp");
    25             //请求转发跳转:可以获取到数据,并且地址栏没有改变(仍然保留转发时的页面)
    26             request.getRequestDispatcher("success.jsp").forward(request, response);
    27         }else{
    28             //登录失败
    29             out.print("用户名或密码错误!");
    30         } */
    31     %>
    32 </body>
    33 </html>
    result.jsp
     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <title>Insert title here</title>
     8 </head>
     9 <body>
    10     仅供测试使用
    11 </body>
    12 </html>

    pageContext(后面讲)

    application(后面讲)

    config(后面讲)

    page(后面讲)

    exception(后面讲)

    8.统一请求的编码request

    get方式请求

    如果出现乱码的解决方法:

    a.统一改每一个变量的编码

    new String(旧编码,新编码)

    1 name = new String(name.getBytes("iso-8859-1"),"utf-8")

    b.修改server.xml,一次性的更改tomcat默认get提交方式的编码(utf-8)

    建议使用tomcat时,首先在server.xml中统一get方式的编码

    1 <Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8"/>
    2 <!--添加URIEncoding="UTF-8"以后所有的get方式都是utf-8-->

    post方式请求

    1 <%
    2     //设置编码
    3     request.setCharacterEncoding("utf-8");
    4 %>

    tomcat7(iso-8859-1)

    tomcat8(utf-8)

     

  • 相关阅读:
    HYSBZ 3813 奇数国
    HYSBZ 4419 发微博
    HYSBZ 1079 着色方案
    HYSBZ 3506 排序机械臂
    HYSBZ 3224 Tyvj 1728 普通平衡树
    Unity 3D,地形属性
    nginx 的naginx 种包含include关键字
    Redis 出现NOAUTH Authentication required解决方案
    mysql 8.0出现 Public Key Retrieval is not allowed
    修改jar包里的源码时候需要注意的问题
  • 原文地址:https://www.cnblogs.com/dream-by-dream/p/11699088.html
Copyright © 2011-2022 走看看