一、初识javaWeb

1、C/S框架

clientsrever 客户端服务器,需要安装下载软件才能使用
例如:杀毒软件(因为要扫描磁盘)、QQ

2、B/S框架

BrowserServer 浏览器服务器,比如淘宝,微信小程序
相对来讲B/S更有优势,降低了对用户本地设备环境的要求。降低了 程序的维护成本。

3、URL即网址

一个完整的URL由协议,主机地址(域名),资源位置,参数组成

4、Web服务器

※Tomcat———-免费,开源

5、tomcat部署Web应用

index.html写入前端内容,在eclipse启动tomcat,最终在网页显示
右键7.0—->add and remobved ——>双击web42——>finished——>右键7.0—->start(保持启动状态)—->打开浏览器,输入地址127.0.0.1:8080/web42

6、tomcat默认端口号8080

由于1个服务只用一个端口号,所以修改端口号有如下两种方法
(1)、
QQ图片20210412102801.png
(2)、
QQ图片20210412102809.png

二、使用jsp生成web界面

JSP9大内置对象

1、out输出对象 4、session会话对象 7、response响应对象
2、page页面对象 5、application全局对象(应用程序对象) 8、config配置对象
3、request请求对象 6、pageContext页面上下文对象 9、exception异常对象
  1. :代表的是范围对象

1、JSP

jsp:java server page,运行在服务器端的java页面,使用HTML嵌套java代码

2、在网页上换行输出代码

  1. <%
  2. String s="沈阳";
  3. for(int i=1;i<5;i++){
  4. //+"<br/>"在网页显示换行输出
  5. out.println(s+i+"<br/>");
  6. }
  7. %>

3、在网页显示当前时间

运用Sting类时的,时间对象SimpleDateFormat

  1. <%
  2. //显示当前的时间
  3. Date date=new Date();
  4. SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  5. String sd=sdf.format(date);
  6. out.println(sd);
  7. %>

4、JSP执行过程

image.png
(1)、JSP文件存储地址
E:\workspace.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\web42
调用jsp文件中jsp文件存储地址
(2)、java文件存储地址
E:\workspace.metadata.plugins\org.eclipse.wst.server.core\tmp0\work\Catalina\localhost\web42\org\apache\jsp\pages
jsp编译生成java文件和执行class文件

5、常见错误提示

404错误—-找不到访问的页面或资源
500错误——JSP页面代码有误
页面无法显示—— 未启动Tomcat

三、jsp获取注册信息

1、提交表单的两种方法get/post

获得单个参数的情况:

  1. <form action="page1-success.jsp" method="post">
  2. 用户名:<input type="text" name="userName"><br/>
  3. 密码:<input type="passWord" name="pwd"><br/>
  4. 邮箱:<input type="text" name="email"><br/>
  5. </form>
  6. 另一页
  7. <!-- 获得page1的三个值 -->
  8. <%
  9. //处理中文乱码解决方案 设置request和response的编码格式
  10. request.setCharacterEncoding("UTF-8");
  11. response.setCharacterEncoding("UTF-8");
  12. String userName=request.getParameter("userName");
  13. String pwd=request.getParameter("pwd");
  14. String email=request.getParameter("email");
  15. %>
  16. 用户名:<%=userName %><br/>
  17. 密码:<%=pwd %><br/>
  18. 邮箱:<%=email %><br/>

由测试看出get和post的区别:
image.png
get安全性低是因为,get方法显示的url可以随意修改;

2、获得多个参数的情况

  1. <form action="page1-success.jsp" method="post">
  2. 用户名:<input type="text" name="userName"><br/>
  3. 密码:<input type="passWord" name="pwd"><br/>
  4. 邮箱:<input type="text" name="email"><br/>
  5. <!-- 传递多个参数 -->
  6. 爱好:<input type="checkbox" name="likes" value="1">篮球
  7. <input type="checkbox" name="likes" value="2">足球
  8. <input type="checkbox" name="likes" value="3">排球
  9. <input type="checkbox" name="likes" value="4">游泳
  10. <input type="submit" value="注册">
  11. </form>
  12. 另一页面
  13. <%
  14. //传递多个参数的情况
  15. String[] likes=request.getParameterValues("likes");
  16. %>
  17. <%--多个参数时需要遍历 --%>
  18. 爱好:<%
  19. //非空判断,以防出现空指针异常
  20. if(likes!=null){
  21. for(int i=0;i<likes.length;i++){
  22. if(likes[i].equals("1")){
  23. out.print("篮球、");
  24. }else if(likes[i].equals("2")){
  25. out.print("足球、");
  26. }else if(likes[i].equals("3")){
  27. out.print("排球、");
  28. }else {
  29. out.print("游泳");
  30. }
  31. %>
  32. <%
  33. } }
  34. %>

3、转发

//转发——跳转界面到index,可以携带参数 /只能转发到本项目地址,地址栏不发生改变,可以传值
//request.getRequestDispatcher(“index.jsp”).forward(request, response);

4、重定向

  1. //重定向方法/可以定位到任何地址,地址栏发生改变,不可以传值
  2. //response.sendRedirect("index.jsp");
  3. //response.sendRedirect("http://www.baidu.com");
  4. 可所以跳转到本项目地址或外部地址
  5. //index.jsp中获得page1的参数
  6. String userName=request.getParameter("userName");
  7. out.print(userName+"<br/>");

5、转发和重定向的区别?

image.png
(1)、转发:URL不变,不会重新发送请求,自身携带请求,可以传值,地址只能是本项目地址;
(2)、重定向:URL变化,会重新发送请求,不携带请求,不能传值,但是可以重定向到任意地址上。

6、setAttribute/getAttrbute

如果用户名输入admin,则在index页面提示不能用admin作为用户名,入过输入其他,就在page1-success上正常显示

  1. <%
  2. request.setCharacterEncoding("UTF-8");
  3. response.setCharacterEncoding("UTF-8");
  4. String userName=request.getParameter("userName");
  5. String pwd=request.getParameter("pwd");
  6. String email=request.getParameter("email");
  7. //传递多个参数的情况
  8. String[] likes=request.getParameterValues("likes");
  9. if(userName.equals("admin")){
  10. request.setAttribute("msg", "不能使用管理员admin作为用户名");
  11. request.getRequestDispatcher("index.jsp").forward(request,response);
  12. }
  13. %>
  14. 用户名:<%=userName %><br/>
  15. 密码:<%=pwd %><br/>
  16. 邮箱:<%=email %><br/>
  17. <%--多个参数时需要遍历 --%>
  18. 爱好:<%
  19. //非空判断,以防出现空指针异常
  20. if(likes!=null){
  21. for(int i=0;i<likes.length;i++){
  22. if(likes[i].equals("1")){
  23. out.print("篮球、");
  24. }else if(likes[i].equals("2")){
  25. out.print("足球、");
  26. }else if(likes[i].equals("3")){
  27. out.print("排球、");
  28. }else {
  29. out.print("游泳");
  30. }
  31. %>
  32. <%
  33. } }
  34. %>
  35. index.jsp
  36. <%
  37. String msg=(String)request.getAttribute("msg");
  38. out.print(msg);
  39. %>

四、会话session

1、登录信息存放到session中

  1. <body>
  2. <%
  3. //判断回到登录界面登录失败字体显示红色
  4. Object msg=request.getAttribute("msg");
  5. if(msg != null){
  6. %>
  7. <p style="color:red"><%=msg %></p>
  8. <%
  9. }
  10. %>
  11. <form action="login-success.jsp" method="post">
  12. 用户名:<input type="text" name="userName"><br/>
  13. 密码:<input type="passWord" name="pwd"><br/>
  14. <input type="submit" value="登录">
  15. </form>
  16. </body>
  17. <body>
  18. <%
  19. //获得
  20. String userName=request.getParameter("userName");
  21. String pwd=request.getParameter("pwd");
  22. if("tom".equals(userName) && "123456".equals(pwd)){
  23. //登录成功--跳转到index,登录前将信息存到session
  24. session.setAttribute("uName", userName);
  25. request.getRequestDispatcher("index.jsp").forward(request, response);
  26. }else{
  27. //登录失败--返回登录界面
  28. request.setAttribute("msg", "登录失败");
  29. request.getRequestDispatcher("login.jsp").forward(request, response);
  30. }
  31. %>
  32. </body>

2、在页面显示欢迎,XXX

由于存到session中,在每个页面填入都可以显示,任何页面随时都可以取
session所用在服务器端,tomcat只要关闭,session清空

  1. <body>
  2. <%
  3. //获得
  4. String userName=request.getParameter("userName");
  5. String pwd=request.getParameter("pwd");
  6. if("tom".equals(userName) && "123456".equals(pwd)){
  7. //登录成功--跳转到index,登录前将信息存到session
  8. session.setAttribute("uName", userName);
  9. request.getRequestDispatcher("index.jsp").forward(request, response);
  10. }else{
  11. //登录失败--返回登录界面
  12. request.setAttribute("msg", "登录失败");
  13. request.getRequestDispatcher("login.jsp").forward(request, response);
  14. }
  15. %>
  16. </body>
  17. 另一页
  18. <body>
  19. <%
  20. //判断回到登录界面登录失败字体显示红色
  21. Object msg=request.getAttribute("msg");
  22. if(msg != null){
  23. %>
  24. <p style="color:red"><%=msg %></p>
  25. <%
  26. }
  27. %>
  28. <form action="login-success.jsp" method="post">
  29. 用户名:<input type="text" name="userName"><br/>
  30. 密码:<input type="passWord" name="pwd"><br/>
  31. <input type="submit" value="登录">
  32. </form>
  33. </body>
  34. 跳转页
  35. <%
  36. //获得session值,目的是显示欢迎XXXX
  37. Object uName=session.getAttribute("uName");
  38. %>
  39. <%--判断目的是显示欢迎XXX 并显示绿色--%>
  40. <%if(uName != null){%>
  41. <p style="color:green">欢迎,<%=uName %></p>
  42. <% } %>
  43. page1页面
  44. </head>
  45. <% //放到page1中
  46. //获得session值,目的是显示欢迎XXXX
  47. Object uName=session.getAttribute("uName");
  48. %>
  49. <%--判断目的是显示欢迎XXX --%>
  50. <%if(uName != null){%>
  51. <p style="color:green">欢迎,<%=uName %></p>
  52. <% } %>
  53. <body>

3、清空session的方法

清空后,任何界面将看不到欢迎内容

  1. <body>
  2. <%
  3. //清空session 任何界面都看不到欢迎内容
  4. //第一种 //返回登录页--重定向
  5. session.invalidate();
  6. response.sendRedirect("login.jsp");
  7. //第二种,删除指定的
  8. //session.removeAttribute("uName");
  9. %>
  10. </body>

4、设置session有效期

在web.xml文件中,添加


20

五、cookie

  1. 在登录成功之前
  2. 判断条件里写入
  3. //写入cookie 实现下次登录前,有默认用户名
  4. Cookie cookie = new Cookie("cookieUser",userName);
  5. response.addCookie(cookie);
  6. login页面中,写入
  7. <body>
  8. <%
  9. //取出cookie
  10. Cookie[] cookies=request.getCookies();
  11. String userName="";
  12. for(int i=0;i<cookies.length;i++){
  13. //找到cookieUser这个值
  14. if(cookies[i].getName().equals("cookieUser")){
  15. userName = cookies[i].getValue();
  16. }
  17. }

六、面试题

1.get和post的区别

使用get方法,参数会显示在URL中,有长度限制,由于可以随意更改URL,所以安全性低,URL可以传播;
使用post方法,参数不会显示在URL中,没有长度限制,安全性高,URL不可以传播;

2.转发和重定向的区别

(1)、转发:URL不变,不会重新发送请求,自身携带请求,可以传值,地址只能是本项目地址;
(2)、重定向:URL变化,会重新发送请求,不携带请求,不能传值,但是可以重定向到任意地址上。

3.session和cookie区别

session作用在服务器端,保存的是对象,会话会随着tomcat的结束而失效,通常保存重要信息,内置对象
cookie作用在客户端,保存的是字符串,可长期保存在客户端。不是内置对象

七、application全局作用域

1、application可实现计数器的功能-

——-哪个页面的访问量高

  1. <body>
  2. <%
  3. //实现计数器,哪个页面的访问次数
  4. Object countObject=application.getAttribute("count");
  5. if(countObject==null){
  6. application.setAttribute("count", 1);
  7. }else{
  8. int count=(Integer)countObject;
  9. application.setAttribute("count", count+1);
  10. }
  11. %>
  12. <h2>页面被访问了多少次<%=application.getAttribute("count") %>次</h2>
  13. </body>

2、作用域的对比

page< request 页面 请求 会话 工程级别
page:只在当前页面有效,一旦离开当前页面,则无法访问;
request:存储数据仅存在一个请求中可用,请求中有单个页面或者多个页面,请求失效才会失效;
session: 会话结束才会失效
application: 用户共享数据,所有人都可以请求,只要web停止才会终止;