【学习笔记】Jsp与Servlet技术
原创
©著作权归作者所有:来自51CTO博客作者codelinghu的原创作品,请联系作者获取转载授权,否则将追究法律责任
Jsp与Servlet技术
一.为什么要学习Jsp?
- 我们以前学的都是静态网页的编程,比如
html
之类的,我们希望静态网页能够有动态特性,则需要使用到jsp
技术。 -
Jsp
页面大体由html代码和java代码组成。
【Demo01】使用Idea配置好Tomacat
服务器,运行jsp项目。
<%--
Created by IntelliJ IDEA.
User: 编程小哥令狐
Date: 2020/7/19
Time: 14:42
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html" charset="GB2312">
<title>第一个Jsp页面</title>
</head>
<body>
<table>
<tr>
<td>id</td>
<td>姓名</td>
</tr>
<%
String color1="99caff";
String color2="88cc33";
for(int i=1;i<=10;i++){
String color="";
if(i%2==0){
color=color1;
}else{
color=color2;
}
out.println("<tr bgcolor="+color+">");
out.println("<td>"+i+"</td>");
out.println("<td>姓名"+i+"</td>");
out.println("</tr>");
}
%>
</table>
</body>
</html>
1.1Jsp工作原理
- Jsp的工作对象分为:客户端,服务器端(包含了jsp引擎)
- Jsp动作方式:请求和响应
- 工作流程与原理:
- 客户端发送数据请求,web服务器接收到,将这个页面请求交给Jsp引擎处理。【常见的引擎Tomacat】。
- Jsp引擎将提交过来的页面准换成java源文件,Jsp源文件。进行编译,解析后生成Html文件发送到服务器端。客户端接收到服务器端响应之后展示信息给用户
1.2实例—提交表单/发送数字/显示内容次数
1.2.1项目目录结构及其代码
- **需求:**用户输入并提交Hello world打印的次数。
- 一个html文件,一个jsp文件
html
部分代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html" charset="UTF-8">
<title>理解JSP工作原理</title>
</head>
<body>
<p>请输入显示的次数:</p>
<form action="index.jsp" method="get">
<input type="text" name="times">
<input type="submit" value="提交">
</form>
</body>
</html>
jsp
部分代码:
<%--
Created by IntelliJ IDEA.
User: 编程小哥令狐
Date: 2020/7/20
Time: 8:03
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html" charset="GB2312">
<title>理解JSP工作原理</title>
</head>
<body>
<h1>
<%
int times=Integer.parseInt(request.getParameter("times"));
for(int i=0;i<times;i++){
out.println("Hello world");
out.println("<br/>");
}
%>
</h1>
</body>
</html>
二.Jsp的构成
三.JSP内建对象
在所有的JSP页面中都需要使用内部对象
常见的额JSP对象有:
out.print()
和 out.println()
方法都可以向客户端发送数据
对象请求,通过 getParameter()
方法可以得到 request
的参数
封装了JSP产生的响应。
保存用户操作状态,用户信息。
保存多个应用对象的信息
3.1使用Request对象案例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" http-equiv="Content-Type" content="text/html">
<title>数据输入</title>
</head>
<body>
<form method="post" action="requstDemo.jsp">
<table>
<tr>
<td>请输入登录名:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>请输入密码:</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td><input type="submit" value="登录"></td>
</tr>
</table>
</form>
</body>
</html>
<%@ page import="javax.xml.namespace.QName" %><%--
Created by IntelliJ IDEA.
User: 编程小哥令狐
Date: 2020/7/20
Time: 17:33
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>request对象的使用</title>
</head>
<body>
Request对象的信息:
<hr>
<%
out.println("<br>getMethod:");
out.println(request.getMethod());
out.println("<br>getParameter:");
out.println(request.getParameterNames());
%>
</body>
</html>
3.2使用JavaBean创建商品信息展示
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-eaL6pswO-1595475011561)(Jsp与Servlet技术.assets/20200721102536.png)]
package demo;
public class CommodityInfoBean {
private String name;
private Float price;
private String status;
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public Float getPrice(){
return price;
}
public void setPrice(Float price) {
this.price = price;
}
public String getStatus(){
return status;
}
public void setStatus(String status) {
this.status = status;
}
public CommodityInfoBean(){
this.name="惠普笔记本电脑CQ515";
this.price=2950.0f;
this.status="在售";
}
}
<%--
Created by IntelliJ IDEA.
User: linghu
Date: 2020/7/21
Time: 1:06
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html" charset="UTF-8">
<title>显示商品信息</title>
</head>
<body>
<jsp:useBean id="commodity" scope="page" class="demo.CommodityInfoBean"/>
<h1>商品信息如下:</h1>
名称:<%=commodity.getName()%>
<br>
价格:<%=commodity.getPrice()%>
<br>
状态:<jsp:getProperty name="commodity" property="status"/>
</body>
</html>
|
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Q9FzQdZA-1595475011985)(Jsp与Servlet技术.assets/20200721102536-1595404378410.png)]
|
Servlet技术
1.1Servlet技术介绍
-
Servlet
是一个Java类 -
Servlet
部署在容器里,他的生命周期由容器管理。
1.1.1Servlet生命周期
生命周期如下几个阶段:
- 装载
Servlet
- 创建
Servlet
实例 - 调用
Servlet
的intit()
方法 - 容器接收到
Servlet
的请求后将调用Service()
方法 - 实例被销毁,利用方法
destory()
1.1.2使用HttpServlet处理客户端请求
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" http-equiv="Content-Type" content="text/html">
<title>使用Get调用传递参数</title>
</head>
<body>
<form action="doGetDemo" method="get">
请输入参数:
<input type="text" name="name"/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
package code1203;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(name = "doGetDemo",urlPatterns = "/doGetDemo")
public class doGetDemo extends HttpServlet {
/*意思bai是定义程序序列化ID。序列化ID等同于du身份验证,主要zhi用于程序的版本dao控制,维护不同版本的兼容性以及避免在程序版本升级时程序报告的错误。*/
private static final long serialVersionUID=1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
request.setCharacterEncoding("gb2312");
response.setContentType("text/html;charset=gb2312");
PrintWriter out=response.getWriter();
out.println("获得一个参数值:name=<br>"+request.getParameter("name"));
out.flush();
}
}
doPost
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" http-equiv="Content-Type" content="text/html">
<title>使用Post调用传递参数</title>
</head>
<body>
<form action="doPostDemo" method="post">
请输入参数:<br>
<label>
<textarea rows="10" cols="50" name="data"></textarea>
</label><br>
<input type="submit" value="提交">
</form>
</body>
</html>
package code1203;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/doPostDemo")
public class doPostDemo extends HttpServlet {
/*意思bai是定义程序序列化ID。序列化ID等同于du身份验证,主要zhi用于程序的版本dao控制,维护不同版本的兼容性以及避免在程序版本升级时程序报告的错误。*/
private static final long serialVersionUID=1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
request.setCharacterEncoding("gb2312");
response.setContentType("text/html;charset=gb2312");
PrintWriter out=response.getWriter();
out.println("获得一个参数值:data=<br>"+request.getParameter("data"));
out.flush();
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-upbQ7tff-1595475011569)(Jsp与Servlet技术.assets/20200721210338.png)]
|
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fDvUWHYH-1595475011987)(Jsp与Servlet技术.assets/20200721210339.png)]
|
1.2Jsp与Servlet开发
**需求:**编写一个简单的Web应用,实现用户登录的功能
JavaBean
+Jsp
+Servlet
package code1205;
public class UserBean {
private String validUserName;
private String validUserPassword;
public String getValidUserName(){
return validUserName;
}
public void setValidUserName(String validUserName){
this.validUserName=validUserName;
}
public String getValidUserPassword(){
return validUserPassword;
}
public void setValidUserPassword(String validUserPassword){
this.validUserPassword=validUserPassword;
}
//验证账户密码
public boolean isValidUser(String name,String password){
boolean result=false;
if(validUserName.equals(name)&&validUserPassword.equals(password)){
result=true;
}
return result;
}
}
package code1205;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.http.HttpResponse;
@WebServlet("/userLogin")
public class UserLogin extends HttpServlet {
private static final long serialVersionUID=1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)throws IOException {
/*
* request.getParameter就是把jsp里的内容读取出来进行封装
* */
String name=request.getParameter("name");
String password=request.getParameter("password");
//设置合法的用户信息
UserBean user=new UserBean();
user.setValidUserName("admin");
user.setValidUserPassword("admin");
response.setCharacterEncoding("gb2312");
response.setContentType("text/html;charset=gb2312");
PrintWriter out=response.getWriter();
if(user.isValidUser(name,password)){//调用javaBean的方法来验证用户
out.println("登录成功");
}else {
out.println("用户登录名或者密码错误,<a href='login.jsp'>请重试</a>");
}
}
protected void doPost(HttpServletRequest request, HttpResponse
response)throws IOException{
doGet(request, (HttpServletResponse) response);
}
}
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2020/7/22
Time: 14:59
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html" charset="UTF-8">
<title>用户登录</title>
</head>
<body>
<form action="userLogin" method="get">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password"/></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="登录"/></td>
</tr>
</table>
</form>
</body>
</html>
| 登录页面 |
| ----------------------------------------------- |
| [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hya5orZK-1595475011990)(Jsp与Servlet技术.assets/20200722160324.png)] |
| 登录成功 |
| ----------------------------------------------- |
| [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-S1oj0t8Y-1595475011991)(Jsp与Servlet技术.assets/20200722160325.png)] |
| 登录失败 |
| ----------------------------------------------- |
| [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lMoyUbLk-1595475011992)(Jsp与Servlet技术.assets/20200722160326.png)] |