【jsp】入门
程序员文章站
2022-06-01 22:24:02
...
再来了解一下jsp吧。。现在好像都不用jsp了
取而代之的是常见的模板引擎,比如thyme leaf
servlet
不适合设置HTML响应体,需要大量的response.getWriter().print("<html>")
动态资源,可以编程
HTML
静态页面,不能包含动态信息
jsp
在HTML基础上添加Java脚本,构成jsp页面
java脚本示例
print方法老不好使,原来要加一个lib,选tomcat就好了
<%--
Created by IntelliJ IDEA.
User: Lenovo
Date: 2020/9/20
Time: 12:04
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
This is my JSP page. <br>
<%
int a = 10;
%>
<%
out.print(a);
%>
<br/>
<%=a %>
<table border="1" align="center" width="60%">
<tr>
<td>name</td>
<td>age</td>
</tr>
<%
for (int i = 0; i < 10; i++) {
%>
<tr>
<td>Tom</td>
<td>33</td>
</tr>
<%
}
%>
</table>
</body>
</html>
jsp与servlet合作
<%--
Created by IntelliJ IDEA.
User: Lenovo
Date: 2020/9/20
Time: 15:43
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>form.jsp</title>
</head>
<body>
<form action="/AServlet" method="post">
整数1 <input type="text" name="num1"/><br>
整数2 <input type="text" name="num2"/><br>
<input type="submit" value="提交">
</form>
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: Lenovo
Date: 2020/9/20
Time: 15:43
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>result.jsp</title>
</head>
<body>
<%
Integer result = (Integer)request.getAttribute("result");
%>
<%=result%>
</body>
</html>
AServlet
package cn.it.web.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author KNOE
* @date 2020-09-20 15:47
*/
public class AServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String s1 = request.getParameter("num1");
String s2 = request.getParameter("num2");
int num1 = Integer.parseInt(s1);
int num2 = Integer.parseInt(s2);
//calculate
int sum = num1 + num2;
//保存到request域中
request.setAttribute("result", sum);
//转换到result.jsp
request.getRequestDispatcher("/add/result.jsp")
.forward(request, response);
}
}
web
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>AServlet</servlet-name>
<servlet-class>cn.it.web.servlet.AServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AServlet</servlet-name>
<url-pattern>/AServlet</url-pattern>
</servlet-mapping>
</web-app>
新建servlet的时候,不要选这个
这样会在xml中自动生成servlet
但是mapping还得自己写
cookie
a.jsp
<%--
Created by IntelliJ IDEA.
User: Lenovo
Date: 2020/9/20
Time: 16:10
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>a.jsp</title>
</head>
<body>
<h1>保存cookie</h1>
<%
Cookie cookie1 = new Cookie("aaa", "AAA");
response.addCookie(cookie1);
Cookie cookie2 = new Cookie("bbb", "BBB");
response.addCookie(cookie2);
%>
</body>
</html>
b.jsp
<%--
Created by IntelliJ IDEA.
User: Lenovo
Date: 2020/9/20
Time: 16:10
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>b.jsp</title>
</head>
<body>
<h1>获取cookie</h1>
<%
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for(Cookie c: cookies){
out.print(c.getName() + "=" + c.getValue() + "<br/>");
}
}
%>
</body>
</html>
再进入b.jsp
session
session底层依赖cookie
a.jsp
<%--
Created by IntelliJ IDEA.
User: Lenovo
Date: 2020/9/20
Time: 16:29
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>a.jsp</title>
</head>
<body>
<h1>向session域保存数据</h1>
<%
//已经创建了session变量
session.setAttribute("aaa", "AAA");
%>
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: Lenovo
Date: 2020/9/20
Time: 16:30
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>b.jsp</title>
</head>
<body>
<h1>获取session中的数据</h1>
<%
String s = (String)session.getAttribute("aaa");
%>
<%=s%>
</body>
</html>
httpSession实例
原来改了src要restart server找了半天没找到问题
LoginServlet
package cn.it.servlet;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;
/**
* @author KNOE
* @date 2020-09-20 16:41
*/
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
//校验
if(!"it".equalsIgnoreCase(username)){
/**
* 把用户名保存到cookie中 发送给客户端浏览器
* 当再次打开login.jsp时 会读取request中的cookie
* 显示到用户名文本框中
*/
Cookie cookie = new Cookie("uname", username);
cookie.setMaxAge(60 * 60 * 2); //设置时长2h
// cookie.setPath("/");
response.addCookie(cookie);
/**
* 正确
* 保存用户信息到session中
* 重定向到succ1.jsp
*/
HttpSession session = request.getSession();
session.setAttribute("username", username);
response.sendRedirect("/session2/succ1.jsp");
}else {
/**
* 错误
* 保存错误信息到request中
* 转发到login.jsp
*/
request.setAttribute("msg", "用户名或密码错误!");
//得到转发器
RequestDispatcher rd = request.getRequestDispatcher("/session2/login.jsp");
rd.forward(request, response); //forward
}
}
}
login
<%--
Created by IntelliJ IDEA.
User: Lenovo
Date: 2020/9/20
Time: 16:41
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>login.jsp</title>
</head>
<body>
<%-- 本页面提供登陆表单 还显示错误信息--%>
<h1>登陆</h1>
<%
/**
* 读取名为uname的cookie
* 如果为空 显示''
* 如果不为空 显示cookie的值
*/
String uname = "";
Cookie[] cs = request.getCookies();
if (cs != null) {
//如果存在cookie
for (Cookie c : cs) {
//查找名为uname的cookie
if("uname".equals(c.getName())) {
uname = c.getValue();
}
out.print(c.getName() + "=" + c.getValue() + "<br/>");
}
}
%>
<%
String message = "";
String msg = (String) request.getAttribute("msg");
if (msg != null) {
message = msg;
}
%>
<font color="red"><b><%=message %></b></font>
<form action="/LoginServlet" method="post">
<%--把cookie中的用户名显示到文本框中--%>
用户名 <input type="text" name="username" value="<%=uname%>"/><br>
密 码 <input type="password" name="password"><br>
<input type="submit" value="登陆">
</form>
</body>
</html>
succ1
<%--
Created by IntelliJ IDEA.
User: Lenovo
Date: 2020/9/20
Time: 16:40
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>succ1.jsp</title>
</head>
<body>
<h1>succ1</h1>
<%
String username = (String)session.getAttribute("username");
if (username == null) {
//向request域中保存错误信息 转发到login.jsp
request.setAttribute("msg", "您还没有登陆 请走正门");
request.getRequestDispatcher("/session2/login.jsp")
.forward(request, response);
return;
}
%>
欢迎<%=username%>!
</body>
</html>
succ2与succ1完全一样,表示由于session存在,不需要重新登陆
web
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>cn.it.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
</web-app>
JavaBean
必须有一个默认构造器
提供get/set方法,如果只有get,那么这个属性是只读的
上一篇: JSP基础知识