JSTL标签、SQL标签、XML标签、格式化标签
1,EL表达式:
表
达
式
例
如
:
{表达式} 例如:
表达式例如:{uaer.sex}等价于
u
s
e
r
[
"
s
e
x
"
]
注
意
:
一
般
情
况
下
,
点
和
中
括
号
通
用
,
但
是
有
二
种
情
况
除
外
,
(
1
)
包
含
特
殊
字
符
{user["sex"]} 注意:一般情况下,点和中括号通用,但是有二种情况除外, (1)包含特殊字符
user["sex"]注意:一般情况下,点和中括号通用,但是有二种情况除外,(1)包含特殊字符{user.first-name} (错误) --------》
u
s
e
r
[
"
f
i
r
s
t
−
n
a
m
e
"
]
(
正
确
)
(
2
)
通
过
变
量
动
态
取
值
:
{user["first-name"]} (正确) (2)通过变量动态取值:
user["first−name"](正确)(2)通过变量动态取值:{user[ param]} (param可以取值name、sex等)
<body>
<% request.setCharacterEncoding("utf-8");
request.setAttribute("password", "123");
%>
用户名:<%=request.getParameter("username") %>
爱 好:<% String[] favors = request.getParameterValues("favor");
for(int i=0;i<favors.length;i++){
out.println(favors[i]+" ");
}
%>
密 码: <%=request.getAttribute("password")%>
请求体MIME类型 <%=request.getContentType() %><br>
协议类型及版本号<%=request.getProtocol() %><br>
服务器主机名<%=request.getServerName() %><br>
服务器端口号<%=request.getServerPort() %><br>
请求文件的长度<%=request.getContentLength() %><br>
请求客户端的IP地址<%=request.getRemoteAddr() %><br>
真实路径<%=request.getRealPath("login.jsp") %><br>
上下文路径<%=request.getContextPath() %><br>
</body>
请求重定向是客户器端行为而请求转发是服务器端行为、
<%
response.setContentType("text/html;charset=utf-8");
out.println("<h1>response</h1>");
out.flush();
PrintWriter outer =response.getWriter();
outer.println("大家好,我是response对象生成的输出流outer对象");
response.sendRedirect("login.jsp");
request.getRequestDispatcher("login.jsp").forward(request,response);
%>
<body>
session的ID编号:<%=session.getId() %><br>
session中获取用户名:<%=session.getAttribute("username") %><br>
session中保存属性<%
String [] names = session.getValueNames();
for(int i=0;i<names.length;i++)
{
out.println(names[i]+" ");
}
%>
</body>
<body>
<%
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日hh:mm:ss");
Date d= new Date(session.getCreationTime());
session.setAttribute("username", "admin");
session.setAttribute("password", "123");
session.setAttribute("age", 20);
//设置当前session最大生成期限单位是秒
session.setMaxInactiveInterval(10);//10秒
%>
session的创建时间:<%=sdf.format(d) %><br>
session的ID编号:<%=session.getId() %><br>
session中获取用户名:<%=session.getAttribute("username") %><br>
<a href="login.jsp" target="_black">跳转到login页面</a>
</body>
<body>
<%
application.setAttribute("username", "admin");
application.setAttribute("password", "123");
application.setAttribute("age", 20);
%>
名字:<%=application.getAttribute("username") %>
application中的属性:
<%
Enumeration attributes = application.getAttributeNames();
while (attributes.hasMoreElements())
{
out.println(attributes.nextElement()+" ");
}
%><br>
JSP引擎名义及版本号:<%=application.getServerInfo() %><br>
</body>
<%@ page language="java" import="java.util.*,java.io.PrintWriter,java.text.*" contentType="text/html; charset=utf-8" errorPage="exception.jsp"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<body>
<%
System.out.println(100/0);
%>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8" isErrorPage="true"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'a.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%=exception.getMessage() %><br>
</body>
</html>
package com;
public class Users {
private String username;
private String password;
public Users(){
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
<%@ page language="java" import="java.util.*" pageEncoding="utf-8" %>
<%@ page import="com.Users" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'a.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
Users user = new Users();
user.setUsername("admin");
user.setPassword("123");
%>
<h1>使用普通方式创建javabean的实例</h1>
用户名:<%=user.getUsername() %>
密 码:<%=user.getPassword() %>
</body>
</html>
<body>
<jsp:useBean id="myusers" class="com.Users"scope="page"></jsp:useBean>
<h1>使用userBean动作创建javabean的实例</h1>
用户名:<%=myusers.getUsername() %>
密 码:<%=myusers.getPassword() %>
</body>
<body>
<jsp:useBean id="myusers" class="com.Users"scope="page"></jsp:useBean>
<h1>setProperty动作元素</h1>
<%-- 1.根据表单自动匹配所有属性 --%>
<jsp:setProperty property="*" name="myusers"/>
<%-- 2.根据表单匹配所有部分的属性 --%>
<jsp:setProperty property="username" name="myusers"/>
用户名:<%=myusers.getUsername() %>
<%-- 3.跟表单无关,通过手动赋值给属性 --%>
<jsp:setProperty property="username" name="myusers" value="lz"/>
<jsp:setProperty property="password" name="myusers" value="123"/>
用户名:<%=myusers.getUsername() %>
密 码:<%=myusers.getPassword() %>
<!--4.通过URL传参数给属性赋值 -->
<jsp:setProperty name="myusers" property="username"/>
<jsp:setProperty name="myusers" property="password" param="mypass"/>
<!-- 使用传统的表达式方式来获取用户名和密码 -->
用户名:<%=myusers.getUsername() %><br>
密码:<%=myusers.getPassword() %><br>
</body>
<body>
<h1>登录系统</h1>
<form name="login" method="post" action="dologin.jsp?mypass=1234" >
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password"/></td>
</tr>
<tr>
<td colspan="2"align="center"><input type="submit" value="登录"/></td>
</tr>
</table>
</form>
</body>
package com.dao;
import com.po.Users;
//用户的业务逻辑类
public class UsersDAO {
//用户登录方法
public boolean usersLogin(Users u)
{
if("admin".equals(u.getUsername())&&"admin".equals(u.getPassword()))
{
return true;
}
else
{
return false;
}
}
}
package com.po;
//用户类
public class Users {
private String username;
private String password;
public Users()
{
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%
request.setCharacterEncoding("utf-8");
%>
<jsp:useBean id="loginUser" class="com.po.Users" scope="page"/>
<jsp:useBean id="userDAO" class="com.dao.UsersDAO" scope="page"/>
<jsp:setProperty property="*" name="loginUser"/>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
//如果用户和密码都等于admin,则登录成功
if(userDAO.usersLogin(loginUser))
{
session.setAttribute("loginUser", loginUser.getUsername());
request.getRequestDispatcher("login_success.jsp").forward(request, response);
}
else
{
response.sendRedirect("login_failure.jsp");
}
%>
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<!-- Page title -->
<title>imooc - Login</title>
<!-- End of Page title -->
<!-- Libraries -->
<link type="text/css" href="css/login.css" rel="stylesheet" />
<link type="text/css" href="css/smoothness/jquery-ui-1.7.2.custom.html" rel="stylesheet" />
<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="js/easyTooltip.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.7.2.custom.min.js"></script>
<!-- End of Libraries -->
</head>
<body>
<div id="container">
<div class="logo">
<a href="#"><img src="assets/logo.png" alt="" /></a>
</div>
<div id="box">
<form action="dologin.jsp" method="post">
<p class="main">
<label>用户名: </label>
<input name="username" value="" />
<label>密码: </label>
<input type="password" name="password" value="">
</p>
<p class="space">
<input type="submit" value="登录" class="login" style="cursor: pointer;"/>
</p>
</form>
</div>
</div>
</body>
</html>
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<!-- Page title -->
<title>imooc - Login</title>
<!-- End of Page title -->
<!-- Libraries -->
<link type="text/css" href="css/login.css" rel="stylesheet" />
<link type="text/css" href="css/smoothness/jquery-ui-1.7.2.custom.html" rel="stylesheet" />
<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="js/easyTooltip.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.7.2.custom.min.js"></script>
<!-- End of Libraries -->
</head>
<body>
<div id="container">
<div class="logo">
<a href="#"><img src="assets/logo.png" alt="" /></a>
</div>
<div id="box">
登录失败!请检查用户或者密码!<br>
<a href="login.jsp">返回登录</a>
</div>
</div>
</body>
</html>
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<!-- Page title -->
<title>imooc - Login</title>
<!-- End of Page title -->
<!-- Libraries -->
<link type="text/css" href="css/login.css" rel="stylesheet" />
<link type="text/css" href="css/smoothness/jquery-ui-1.7.2.custom.html" rel="stylesheet" />
<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="js/easyTooltip.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.7.2.custom.min.js"></script>
<!-- End of Libraries -->
</head>
<body>
<div id="container">
<div class="logo">
<a href="#"><img src="assets/logo.png" alt="" /></a>
</div>
<div id="box">
登录失败!请检查用户或者密码!<br>
<a href="login.jsp">返回登录</a>
</div>
</div>
</body>
</html>
<%@ page language="java" import="java.util.*,java.net.*" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>" rel="external nofollow" rel="external nofollow" >
<title>My JSP 'dologin.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css" rel="external nofollow" rel="external nofollow" >
-->
</head>
<body>
<h1>登录成功</h1>
<%
request.setCharacterEncoding("utf-8");
//首先判断用户是否记住登录状态
String[] isusercookie = request.getParameterValues("isusercookie");
if (isusercookie!=null&&isusercookie.length>0)
{
//用户名和密码保存在cookie对象中
String username = URLEncoder.encode(request.getParameter("username"),"utf-8");
//使用 URLEncoder.encode解决无法在cookie当中保存中文字符串问题
String password = URLEncoder.encode(request.getParameter("password"),"utf-8");
Cookie usernameCookie = new Cookie("username",username);
Cookie passwordCookie = new Cookie("password",password);
usernameCookie.setMaxAge(864000);
passwordCookie.setMaxAge(864000);
response.addCookie(usernameCookie);
response.addCookie(passwordCookie);
}
else
{
Cookie [] cookies = request.getCookies();
if(cookies!=null&&cookies.length>0)
{
for(Cookie c:cookies)
{
if(c.getName().equals("uasename")||c.getName().equals("password"))
{
c.setMaxAge(0); //设置cookie失效
response.addCookie(c); //重新保存
}
}
}
}
%>
<a href="users.jsp" target="_black">查看用户信息</a>
</body>
</html>
<%@ page language="java" import="java.util.*,java.net.*" contentType="text/html; charset=utf-8" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>" rel="external nofollow" rel="external nofollow" >
<title>My JSP 'login.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css" rel="external nofollow" rel="external nofollow" >
-->
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String username = "";
String password = "";
Cookie [] cookies = request.getCookies();
if(cookies!=null&&cookies.length>0)
{
for(Cookie c:cookies)
{
if(c.getName().equals("uasename"))
{
username =URLDecoder.decode(c.getValue(),"utf-8");
}
if(c.getName().equals("password"))
{
password = URLDecoder.decode(c.getValue(),"utf-8");
}
}
}
%>
<h1>系统登录</h1>
<hr>
<form name="loginForm" action="dologin.jsp" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="username" value="<%=username%>"/></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password" value="<%=password%>"/></td>
</tr>
<tr>
<td colspan="2"><input type="checkbox" name="isusercookie" checked="checked" />十天内记住我的登录状态</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="登录"/></td>
</tr>
</table>
</form>
</body>
</html>
<%@ page language="java" import="java.util.*,java.io.PrintWriter,java.text.*,java.net.*" contentType="text/html; charset=utf-8" errorPage="exception.jsp"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<h1>用户信息</h1>
<body>
<%
request.setCharacterEncoding("utf-8");
String username = "";
String password = "";
Cookie [] cookies = request.getCookies();
if(cookies!=null&&cookies.length>0)
{
for(Cookie c:cookies)
{
if(c.getName().equals("username"))
{
username = URLDecoder.decode(c.getValue(),"utf-8");
}
if(c.getName().equals("password"))
{
password = URLDecoder.decode(c.getValue(),"utf-8");
}
}
}
%>
用户名:<%=username %> <br>
密 码:<%=password %> <br>
</body>
</html>