云虚拟主机建设网站一定要域名,基础展示型网站和cms,wordpress怎么添加ico,网站降权不收录目录 ServletServlet的三大职责跳转#xff1a;请求转发和重定向请求转发重定向汇总请求转发与重定向的区别用请求转发和重定向完善登录 JSP第一个JSP概述注释设置创建JSP文件默认字符编码集 JSP的java代码书写JSP的原理三大指令九大内置对象改造动态web工程进行示例内置对象名… 目录 ServletServlet的三大职责跳转请求转发和重定向请求转发重定向汇总请求转发与重定向的区别用请求转发和重定向完善登录 JSP第一个JSP概述注释设置创建JSP文件默认字符编码集 JSP的java代码书写JSP的原理三大指令九大内置对象改造动态web工程进行示例内置对象名称来源名单列表 四大作用域概述案例测试登录完善 Servlet
Servlet的三大职责
1.接受参数 -- req.getParameter 非必须 2.处理业务 -- 拿到数据后去做一些事情非必须 3.跳转必须– 操作完的一个结果 两句代码
跳转请求转发和重定向 请求转发
案例演示动态web项目
AServlet
WebServlet(/go/a)
public class AServlet extends HttpServlet{Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println(AServlet);String name req.getParameter(name);System.out.println(A-name: name);req.setAttribute(password, 123456);// 请求转发req.getRequestDispatcher(/go/b).forward(req, resp);}
}BServlet
WebServlet(/go/b)
public class BServlet extends HttpServlet{Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println(BServlet);String name req.getParameter(name);System.out.println(B-name: name);String password (String) req.getAttribute(password);System.out.println(B-password: password);}
}浏览器访问http://localhost/go/a?namezhangsan
控制台
AServlet
A-name: zhangsan
BServlet
B-name: zhangsan
B-password: 123456req.getRequestDispatcher(“路径”).forward(request, response); 请求里的东西forward可以理解为携带
带值跳转可以访问WEB-INF中资源地址栏不改变 发送一次请求最后一个response起作用不可以跨域[跨网站]访问
重定向
案例演示动态web项目
CServlet
WebServlet(/go/c)
public class CServlet extends HttpServlet{Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println(CServlet);String name req.getParameter(name);System.out.println(C-name: name);resp.sendRedirect(/go/d);}
}DServlet
WebServlet(/go/d)
public class DServlet extends HttpServlet{Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println(DServlet);String name req.getParameter(name);System.out.println(D-name: name);}
}浏览器访问http://localhost/go/c?namezhangsan
控制台
CServlet
C-name: zhangsan
DServlet
D-name: nullresp.sendRedirect(“路径”) 响应里的东西可以有避免重复扣款和访问外部网站之类的作用
无法带值不能访问WEB-INF下内容地址栏改变 两次请求起作用的依然是最后一个可以跨域访问
汇总 请求转发与重定向的区别
请求转发的特点可以携带参数只用一次请求可以访问WEB-INF 重定向的特点可以避免重复扣款场景风险可以访问外部网站不能访问WEB-INF
请求转发过程浏览器 - 内部代码 - WEB-INF 重定向过程浏览器 - 内部代码 - 浏览器 - URL
动态web项目示例 EServlet
WebServlet(/go/e)
public class EServlet extends HttpServlet{Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println(EServlet);System.out.println(E----扣款1000);// req.getRequestDispatcher(/go/f).forward(req, resp);
// resp.sendRedirect(/go/f);
// resp.sendRedirect(https://www.fu365.com/);// req.getRequestDispatcher(/WEB-INF/haha.html).forward(req, resp);
// resp.sendRedirect(/WEB-INF/haha.html);}
}FServlet
WebServlet(/go/f)
public class FServlet extends HttpServlet{Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println(FServlet);}
}WEB-INF下新建haha.html 浏览器访问http://localhost/go/e
用请求转发和重定向完善登录
webapp下WEB-INF外新建login.html
!DOCTYPE html
html
head
meta charsetUTF-8
titleInsert title here/title
/head
bodyform action/loginTest methodpost账号input typetext namenamebr密码input typepassword namepasswordinput typesubmit valuepost/form
/body
/htmlloginTest
WebServlet(/loginTest)
public class LoginServletTest extends HttpServlet{Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {req.setCharacterEncoding(UTF-8);resp.setContentType(text/html;charsetutf-8);String name req.getParameter(name);String password req.getParameter(password);if ( name.equals(zhangsan) password.equals(123456) ) {resp.sendRedirect(main.html); //这里在WEB-INF外重定向可以访问} else {req.setAttribute(msg, 登录失败);// 需要访问另外一个servlet把参数传进页面打印出来req.getRequestDispatcher(/AAAServlet).forward(req, resp);}}
}main.html
!DOCTYPE html
html
head
meta charsetUTF-8
titleInsert title here/title
/head
bodyh1登录成功/h1
/body
/htmlAAAServlet
WebServlet(/AAAServlet)
public class AAAServlet extends HttpServlet{Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {Object attribute req.getAttribute(msg);System.out.println(attribute);PrintWriter writer resp.getWriter();writer.print(!DOCTYPE html);writer.print(html);writer.print(head);writer.print(meta charset\UTF-8\);writer.print(titleInsert title here/title);writer.print(/head);writer.print(body);writer.print(attribute);writer.print( form action\/loginTest\ method\post\);writer.print( 账号:input type\text\ name\username\br);writer.print( 密码:input type\password\ name\password\br);writer.print( input type\submit\ value\post\);writer.print( /form);writer.print(/body);writer.print(/html);}
}JSP
第一个JSP
概述
servlet是用来写java代码的也可以用来做页面展示(把html代码一行一行打印出去)但是不擅长做页面展示。 html用来做页面展示静态网页没办法拿到Java代码不能展示数据。 jsp看起来像html但是它里面可以写java代码动态网页。
注释
webapp下新建_01hello.jsp
% page languagejava contentTypetext/html; charsetUTF-8pageEncodingUTF-8%
!DOCTYPE html PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN http://www.w3.org/TR/html4/loose.dtd
html
head
meta http-equivContent-Type contenttext/html; charsetUTF-8
titleInsert title here/title
/head
body!-- 不安全的注释能在控制台看到 --%-- 安全的注释不能再控制台看到 --%h1我是第一个JSP/h1
/body
/html设置创建JSP文件默认字符编码集
JSP文件内右键 - Preferences - utf-8
JSP的java代码书写
% page languagejava contentTypetext/html; charsetUTF-8pageEncodingUTF-8%
!DOCTYPE html PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN http://www.w3.org/TR/html4/loose.dtd
html
head
meta http-equivContent-Type contenttext/html; charsetUTF-8
titleInsert title here/title
/headbody!-- jsp写java代码的第一种方式打印到后端控制台 --%for(int i 0;i5;i){System.out.println(i: i);}int b 520;%!-- jsp写java代码的第二种方式显示在页面上 --%b %!-- jsp写java代码的第三种方式涉及JSP的底层原理 --%!String ss abc;// System.out.println(abc: ss);%/body
/htmlJSP的原理
jsp需要tomcat运行才能正常展示内容 访问JSP - tomcat的web.xml - 两个servlet类(把JSP转化为servlet/java文件把html代码打印出去)
tipstomcat的web.xml是全局的项目中的web.xml是局部的
三大指令
1.page 当前页面的一些配置jsp生成java文件时会引用这些配置
% page languagejava contentTypetext/html; charsetUTF-8pageEncodingUTF-8%2.taglib不讲 (下一节来说 )
3.include引用一个文件常用于导航栏 _03include.jsp
% page languagejava contentTypetext/html; charsetUTF-8pageEncodingUTF-8%%include filehead.jsp %
!DOCTYPE html PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN http://www.w3.org/TR/html4/loose.dtd
html
head
meta http-equivContent-Type contenttext/html; charsetUTF-8
titleInsert title here/title
/head
bodydiv螺旋丸/div
/body
/html_04include.jsp
% page languagejava contentTypetext/html; charsetUTF-8pageEncodingUTF-8%!DOCTYPE html PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN http://www.w3.org/TR/html4/loose.dtd
html
head
meta http-equivContent-Type contenttext/html; charsetUTF-8
titleInsert title here/title
/head
bodydiv千年杀/div%include filehead.jsp %
/body
/htmlhead.jsp
% page languagejava contentTypetext/html; charsetUTF-8pageEncodingUTF-8%div stylebackground-color:red;text-align:center;font-size:50px火影忍者/div九大内置对象
改造动态web工程进行示例
改造LoginServletTest登录失败后跳转到login.jsp
WebServlet(/loginTest)
public class LoginServletTest extends HttpServlet{Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {req.setCharacterEncoding(UTF-8);resp.setContentType(text/html;charsetutf-8);String name req.getParameter(name);String password req.getParameter(password);if ( name.equals(zhangsan) password.equals(123456) ) {resp.sendRedirect(main.html); //这里在WEB-INF外重定向可以访问} else {req.setAttribute(msg, 登录失败);// 需要访问另外一个servlet把参数传进页面打印出来
// req.getRequestDispatcher(/AAAServlet).forward(req, resp);req.getRequestDispatcher(login.jsp).forward(req, resp);}}
}webapp下新增login.jsp
% page languagejava contentTypetext/html; charsetUTF-8pageEncodingUTF-8%
!DOCTYPE html PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN http://www.w3.org/TR/html4/loose.dtd
html
head
meta http-equivContent-Type contenttext/html; charsetUTF-8
titleInsert title here/title
/head
body%request.getAttribute(msg) %form action/loginTest methodpost账号input typetext namenamebr密码input typepassword namepasswordinput typesubmit valuepost/form
/body
/html内置对象名称来源
来自tomcat根据jsp生成的java文件在那里面定义了
名单列表
HttpServletRequest request 请求对象
HttpServletResponse response 响应对象
ServletConfig config 配置对象
ServletContext application
Throwable exception 异常( 你当前页面是错误页时才有 isErrorPagetrue )
JspWriter out 输出流对象
Object page 相当于this 是当前页的意思
PageContext pageContext 没好大用处
HttpSession session 会话对象重要四大作用域
概述
HttpServletRequest request 一次请求
HttpSession session 一次会话 同一个浏览器访问tomcat就是一次会话
PageContext pageContext 当前页面 作用不大
ServletContext application 整个会话tomcat没有关闭就不会消失在不同的浏览器都能拿到案例测试
webapp下新增_05page.jsp
% page languagejava contentTypetext/html; charsetUTF-8pageEncodingUTF-8%
!DOCTYPE html PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN http://www.w3.org/TR/html4/loose.dtd
html
head
meta http-equivContent-Type contenttext/html; charsetUTF-8
titleInsert title here/title
/head
body%pageContext.setAttribute(iampageContext,我是当前页对象);request.setAttribute(iamrequest, 我是请求对象);session.setAttribute(iamsession, 我是会话对象);application.setAttribute(iamapplication, 我是应用对象);%%pageContext.getAttribute(iampageContext)%%request.getAttribute(iamrequest)%%session.getAttribute(iamsession)% %application.getAttribute(iamapplication)%
/body
/htmlwebapp下新增_06page.jsp
% page languagejava contentTypetext/html; charsetUTF-8pageEncodingUTF-8%
!DOCTYPE html PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN http://www.w3.org/TR/html4/loose.dtd
html
head
meta http-equivContent-Type contenttext/html; charsetUTF-8
titleInsert title here/title
/head
body%pageContext.getAttribute(iampageContext)%%request.getAttribute(iamrequest)%%session.getAttribute(iamsession)% %application.getAttribute(iamapplication)%
/body
/html启动tomcat浏览器访问http://localhost/_05page.jsp我是当前页对象 我是请求对象 我是会话对象 我是应用对象
浏览器访问http://localhost/_06page.jspnull null 我是会话对象 我是应用对象
换一个浏览器访问http://localhost/_06page.jspnull null null 我是应用对象
重启原浏览器访问http://localhost/_06page.jspnull null null 我是应用对象
重启tomcat访问http://localhost/_06page.jspnull null null null
修改_05page.jsp
% page languagejava contentTypetext/html; charsetUTF-8pageEncodingUTF-8%
!DOCTYPE html PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN http://www.w3.org/TR/html4/loose.dtd
html
head
meta http-equivContent-Type contenttext/html; charsetUTF-8
titleInsert title here/title
/head
body%pageContext.setAttribute(iampageContext,我是当前页对象);request.setAttribute(iamrequest, 我是请求对象);session.setAttribute(iamsession, 我是会话对象);application.setAttribute(iamapplication, 我是应用对象);%%request.getRequestDispatcher(_06page.jsp).forward(request, response);%
/body
/html启动tomcat浏览器访问http://localhost/_05page.jspnull 我是请求对象 我是会话对象 我是应用对象
修改_05page.jsp
% page languagejava contentTypetext/html; charsetUTF-8pageEncodingUTF-8%
!DOCTYPE html PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN http://www.w3.org/TR/html4/loose.dtd
html
head
meta http-equivContent-Type contenttext/html; charsetUTF-8
titleInsert title here/title
/head
body%pageContext.setAttribute(iampageContext,我是当前页对象);request.setAttribute(iamrequest, 我是请求对象);session.setAttribute(iamsession, 我是会话对象);application.setAttribute(iamapplication, 我是应用对象);%%//request.getRequestDispatcher(_06page.jsp).forward(request, response);response.sendRedirect(_06page.jsp);%
/body
/html启动tomcat浏览器访问http://localhost/_05page.jspnull null 我是会话对象 我是应用对象
修改_06page.jsp
% page languagejava contentTypetext/html; charsetUTF-8pageEncodingUTF-8%
!DOCTYPE html PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN http://www.w3.org/TR/html4/loose.dtd
html
head
meta http-equivContent-Type contenttext/html; charsetUTF-8
titleInsert title here/title
/head
body%pageContext.getAttribute(iampageContext)%%request.getAttribute(iamrequest)%%session.getAttribute(iamsession)% %application.getAttribute(iamapplication)%%request.getRequestDispatcher(_05page.jsp).forward(request, response);%
/body
/html启动tomcat浏览器访问http://localhost/_05page.jsp报错该网页无法正常运作localhost将您重定向的次数过多。
登录完善
改造login.jsp
% page languagejava contentTypetext/html; charsetUTF-8pageEncodingUTF-8 isErrorPagetrue %
!DOCTYPE html PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN http://www.w3.org/TR/html4/loose.dtd
html
head
meta http-equivContent-Type contenttext/html; charsetUTF-8
titleInsert title here/title
/head
body%if(request.getAttribute(msg)!null){ %%request.getAttribute(msg) %%} %form action/loginTest methodpost账号input typetext namenamebr密码input typepassword namepasswordinput typesubmit valuepost/form
/body
/htmlLoginServletTest设置数据到session作用域
WebServlet(/loginTest)
public class LoginServletTest extends HttpServlet{Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {ServletContext servletContext req.getServletContext();HttpSession session req.getSession();req.setCharacterEncoding(UTF-8);resp.setContentType(text/html;charsetutf-8);String name req.getParameter(name);String password req.getParameter(password);if ( name.equals(zhangsan) password.equals(123456) ) {session.setAttribute(name, zhangsan);resp.sendRedirect(main.jsp); //这里在WEB-INF外重定向可以访问} else {req.setAttribute(msg, 登录失败);// 需要访问另外一个servlet把参数传进页面打印出来
// req.getRequestDispatcher(/AAAServlet).forward(req, resp);req.getRequestDispatcher(login.jsp).forward(req, resp);}}
}新建main.jsp
% page languagejava contentTypetext/html; charsetUTF-8pageEncodingUTF-8%
!DOCTYPE html PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN http://www.w3.org/TR/html4/loose.dtd
html
head
meta http-equivContent-Type contenttext/html; charsetUTF-8
titleInsert title here/title
/head
body
恭喜你登录成功%session.getAttribute(name)%
/body
/html