QL脚本:
CREATE TABLE `t_userinfo` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`account` varchar(32) DEFAULT '',
`password` varchar(32) DEFAULT NULL,
`email` varchar(255) DEFAULT NULL,
`question` varchar(255) DEFAULT NULL,
`result` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
Java代码:
package com.demo.dao;
import com.demo.model.UserInfo;
public interface UserInfoDao {
/**
* 保存用户信息
* @param userinfo
* @return
*/
public boolean saveUserInfo(UserInfo userinfo);
public UserInfo queryUserInfo(String account);
public boolean updateUserInfo(UserInfo userInfo);
}
package com.demo.dao;
import com.demo.model.UserInfo;
import com.demo.tools.JDBConnection;
import java.sql.ResultSet;
import java.sql.SQLException;
public class UserInfoDaoImp implements UserInfoDao{
private JDBConnection connection=null;
@Override
public boolean saveUserInfo(UserInfo userinfo) {
connection=new JDBConnection();
boolean flag=false;
StringBuilder sql=new StringBuilder();
sql.append("insert into t_userinfo (account,password,email,question,result) values(?,?,?,?,?)");
String[] pam={userinfo.getAccount(),userinfo.getPassword(),userinfo.getEmail(),userinfo.getQuestion(),userinfo.getResult()};
if (connection.updateData(sql.toString(),pam)) {
flag=true;
}
return flag;
}
@Override
public UserInfo queryUserInfo(String account) {
UserInfo userinfo=null;
connection=new JDBConnection();
try {
String sql="select * from t_userinfo where account=?";
String[] pam={account};
ResultSet rs=connection.queryByPsStatement(sql,pam);
while (rs.next()) {
userinfo=new UserInfo();
userinfo.setId(rs.getInt(1));
userinfo.setAccount(rs.getString(2));
userinfo.setPassword(rs.getString(3));
userinfo.setEmail(rs.getString(4));
userinfo.setQuestion(rs.getString(5));
userinfo.setResult(rs.getString(6));
}
}
catch (SQLException ex) {
return null;
}
finally {
connection.closeAll();
}
return userinfo;
}
@Override
public boolean updateUserInfo(UserInfo userInfo) {
connection=new JDBConnection();
boolean flag=false;
StringBuilder sql=new StringBuilder();
sql.append("update into t_userinfo set password=? where id=?");
String[] pam={userInfo.getPassword(),String.valueOf(userInfo.getId())};
if (connection.updateData(sql.toString(),pam)) {
flag=true;
}
return flag;
}
}
package com.demo.model;
public class UserInfo {
private Integer id=-1;
private String account="";
private String password="";
private String email="";
private String question="";
private String result="";
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id=id;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account=account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password=password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email=email;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question=question;
}
public String getResult() {
return result;
}
public void setResult(String result) {
this.result=result;
}
}
package com.demo.servlet;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
public class PictureCheckCode extends HttpServlet {
private Font mFont=new Font("黑体", Font.BOLD, 17);
public PictureCheckCode() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void init() throws ServletException {
// Put your code here
super.init();
}
Color getRandColor(int fc, int bc) {
Random random=new Random();
if (fc > 255)
fc=255;
if (bc > 255)
bc=255;
int r=fc + random.nextInt(bc - fc);
int g=fc + random.nextInt(bc - fc);
int b=fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "No-cache");
response.setDateHeader("Expires", 0);
// 指定生成的响应是图片
response.setContentType("image/jpeg");
int width=70;
int height=19;
BufferedImage image=new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g=image.getGraphics();
Random random=new Random();
g.setColor(getRandColor(200, 250));
g.fillRect(1, 1, width - 1, height - 1);
g.setColor(new Color(102, 102, 102));
g.drawRect(0, 0, width - 1, height - 1);
g.setFont(mFont);
g.setColor(getRandColor(160, 200));
// 画随机的线条
for (int i=0; i < 155; i++) {
int x=random.nextInt(width - 1);
int y=random.nextInt(height - 1);
int x1=random.nextInt(6) + 1;
int y1=random.nextInt(12) + 1;
g.drawLine(x, y, x + x1, y + y1);
}
String sRand="";
// 输出随机的验证文字
for (int i=0; i < 4; i++) {
int itmp=random.nextInt(26) + 65;
char ctmp=(char) itmp;
sRand +=String.valueOf(ctmp);
g.setColor(new Color(20 + random.nextInt(110), 20 + random
.nextInt(110), 20 + random.nextInt(110)));
g.drawString(String.valueOf(ctmp), 15 * i + 10, 16);
}
// 将生成的验证码保存到Session中
HttpSession session=request.getSession(true);
session.setAttribute("rand", sRand);
g.dispose();
ImageIO.write(image, "JPEG", response.getOutputStream());
}
}
package com.demo.servlet;
import com.demo.dao.UserInfoDao;
import com.demo.dao.UserInfoDaoImp;
import com.demo.model.UserInfo;
import com.sun.mail.util.MailSSLSocketFactory;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;
public class UserInfoServlet extends HttpServlet {
private int method;
private UserInfo userInfo=null;
private UserInfoDao userInfoDaoImp=null;
private HttpSession session=null;
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
method=Integer.parseInt(request.getParameter("method"));
if (method==1) {
this.checkAccount(request, response);
}
if (method==2) {
this.register(request, response);
}
if (method==3) {
this.findPS1(request, response);
}
if (method==4) {
this.findPS2(request, response);
}
}
private void sendEmail(String content) {
try {
String from="sk666888@sohu.com";
String to="wgh717@sohu.com";
String subject="找回密码";
String messageText=content;
String password="111111";
String mailserver="smtp.qq.com";
Properties prop=new Properties();
// 设置邮件服务器主机名
prop.put("mail.smtp.host", mailserver);
// 发送服务器需要身份验证
prop.put("mail.smtp.auth", "true");
MailSSLSocketFactory sf=new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
prop.put("mail.smtp.ssl.enable", "true");
prop.put("mail.smtp.ssl.socketFactory", sf);
Session sess=Session.getInstance(prop);
sess.setDebug(true);
MimeMessage message=new MimeMessage(sess);
// 给消息对象设置收件人、发件人、主题、发信时间
InternetAddress mail_from=new InternetAddress(from);
message.setFrom(mail_from);
InternetAddress mail_to=new InternetAddress(to);
message.setRecipient(Message.RecipientType.TO, mail_to);
message.setSubject(subject);
message.setSentDate(new Date());
// 新建一个MimeMultipart对象来存放多个BodyPart对象
Multipart mul=new MimeMultipart();
// 新建一个存放信件内容的BodyPart对象
BodyPart mdp=new MimeBodyPart();
mdp.setContent(messageText, "text/html;charset=gb2312");
// 将含有信件内容的BodyPart加入到MimeMulitipart对象中
mul.addBodyPart(mdp);
// 把mul作为消息对象的内容
message.setContent(mul);
message.saveChanges();
Transport transport=sess.getTransport("smtp");
// 以smtp方式登陆邮箱,第1个参数是发送邮件用的邮件服务器SMTP地址,第2个参数为用户名,第3个参数为密码
transport.connect(mailserver, from, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
catch (Exception e) {
System.out.println("发送邮件产生的错误:" + e.getMessage());
}
}
private void findPS2(HttpServletRequest request,
HttpServletResponse response) throws UnsupportedEncodingException,
ServletException, IOException {
userInfoDaoImp=new UserInfoDaoImp();
String account=new String(request.getParameter("account").getBytes(
"ISO8859_1"), "UTF-8");
UserInfo userInfo=userInfoDaoImp.queryUserInfo(account);
String result=new String(request.getParameter("result").getBytes(
"ISO8859_1"), "UTF-8");
if (result.equals(userInfo.getResult())) {
String password="111";
String enPasswor=com.demo.tools.Encrypt.encodeMD5(password);
userInfo.setPassword(enPasswor);
userInfoDaoImp.updateUserInfo(userInfo);
String content="您的密码是:" + password + ",请您牢记!";
sendEmail(content);
request.setAttribute("information", "已经将您的密码发送到邮箱!");
}
else {
request.setAttribute("information", "您输入的密码答案不正确!");
}
request.getRequestDispatcher("findPS2.jsp").forward(request, response);
}
private void findPS1(HttpServletRequest request,
HttpServletResponse response) throws UnsupportedEncodingException,
ServletException, IOException {
userInfoDaoImp=new UserInfoDaoImp();
String account=new String(request.getParameter("account").getBytes(
"ISO8859_1"), "gb2312");
UserInfo userInfo=userInfoDaoImp.queryUserInfo(account);
if (null !=userInfo) {
request.setAttribute("userInfo", userInfo);
request.getRequestDispatcher("findPS2.jsp").forward(request, response);
} else {
request.setAttribute("information", "您输入用户并不存在!");
request.getRequestDispatcher("findPS1.jsp").forward(request, response);
}
}
private void checkAccount(HttpServletRequest request,
HttpServletResponse response) throws UnsupportedEncodingException,
ServletException, IOException {
String information="";
userInfoDaoImp=new UserInfoDaoImp();
session=request.getSession();
String rand=(String) session.getAttribute("rand");
String code=(String) request.getParameter("code");
String account=request.getParameter("account");
String password=request.getParameter("password");
if (com.demo.tools.Encrypt.isValidInput(account)
&& com.demo.tools.Encrypt.isValidInput(password)) {
account=new String(account.getBytes("ISO8859_1"), "gb2312");
password=com.demo.tools.Encrypt.encodeMD5(password);
}
else {
information="您输入的用户或密码存在非法字符串";
}
if (information.equals("")) {
if (rand.equals(code)) {
userInfo=userInfoDaoImp.queryUserInfo(account);
if (null !=userInfo) {
if (userInfo.getPassword().equals(password)) {
session.setAttribute("userInfo", userInfo);
}
else {
information="您输入的密码不正确";
}
}
else {
information="您输入的用户名不存在!";
}
}
else {
information="您输入的验证码不正确!";
}
}
System.out.println(information);
request.setAttribute("information", information);
request.getRequestDispatcher("index.jsp").forward(request, response);
}
private void register(HttpServletRequest request,
HttpServletResponse response) throws UnsupportedEncodingException,
ServletException, IOException {
String information="";
response.setContentType("text/html;charset=UTF-8");
PrintWriter out=response.getWriter();
userInfo=new UserInfo();
userInfoDaoImp=new UserInfoDaoImp();
session=request.getSession();
String rand=(String) session.getAttribute("rand");
String code=(String) request.getParameter("code");
String account=request.getParameter("account");
String password=request.getParameter("password");
if (com.demo.tools.Encrypt.isValidInput(account)
&& com.demo.tools.Encrypt.isValidInput(password)) {
account=new String(account.getBytes("ISO8859_1"), "UTF-8");
password=com.demo.tools.Encrypt.encodeMD5(password);
}
else {
information="您输入的用户或密码存在非法字符串";
}
if (information.equals("")) {
if (rand.equals(code)) {
userInfo.setAccount(account);
userInfo.setPassword(password);
userInfo.setEmail(request.getParameter("email"));
userInfo.setQuestion(new String(request
.getParameter("question").getBytes("ISO8859_1"),
"UTF-8"));
userInfo.setResult(new String(request.getParameter("result")
.getBytes("ISO8859_1"), "UTF-8"));
System.out.println(userInfo);
if (!userInfoDaoImp.saveUserInfo(userInfo)) {
information="您输入的用户名已经存在!";
}
}
else {
information="您输入的校验码不正确!";
}
if (information.equals("")) {
information="您注册成功!";
request.setAttribute("information", information);
request.getRequestDispatcher("register.jsp").forward(request, response);
}
else {
out.print("<script language=javascript>alert('" + information
+ "');window.location.href='register.jsp';</script>");
}
}
else {
out.print("<script language=javascript>alert('" + information
+ "');window.location.href='register.jsp';</script>");
}
}
}
package com.demo.tools;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class Encrypt {
public static String encodeMD5(String str) {
if (null==str) {
return null;
}
StringBuilder sb=new StringBuilder();
try {
MessageDigest code=MessageDigest.getInstance("MD5");
code.update(str.getBytes());
byte[] bs=code.digest();
for (int i=0; i < bs.length; i++) {
int v=bs[i] & 0xFF;
if (v < 16) {
sb.append(0);
}
sb.append(Integer.toHexString(v));
}
}
catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return sb.toString().toUpperCase();
}
public static boolean isValidInput(String str) {
return str.matches("[a-z0-9]+");
}
}
package com.demo.tools;
import java.sql.*;
public class JDBConnection {
private final String dbDriver="com.mysql.jdbc.Driver";
private final String url="jdbc:mysql://localhost:3306/testdb?useUnicode&characterEncodiing=utf-8&useSSL=true&rewriteBatchedStatements=true";
private final String userName="root";
private final String password="root";
private ResultSet rs=null;
private Statement stmt=null;
private Connection con=null;
private PreparedStatement preparedStatement=null;
public JDBConnection() {
try {
Class.forName(dbDriver).newInstance();
}
catch (Exception ex) {
System.out.println("数据库加载失败");
}
}
private boolean creatConnection() {
try {
con=DriverManager.getConnection(url, userName, password);
con.setAutoCommit(true);
return true;
}
catch (SQLException e) {
System.out.println(e.getMessage());
return false;
}
}
private void createPsStatement(String sql) {
creatConnection();
try {
System.out.println("创建PrepareStatement通道对象。。。");
preparedStatement=con.prepareStatement(sql);
}
catch (SQLException e) {
System.out.println("创建PrepareStatement通道对象失败。。。");
e.printStackTrace();
}
}
private void bundle(String[] parm) {
//判断数组Parm是否为空
if (parm !=null) {
//通过for循环将参数绑定起来
for (int i=0; i < parm.length; i++) {
try {
System.out.println( "进行参数的绑定。。。");
preparedStatement.setString(i + 1, parm[i]);
}
catch (SQLException e) {
System.out.println("绑定参数失败。。。");
e.printStackTrace();
}
}
}
}
public ResultSet queryByPsStatement(String sql,String[] pram) {
createPsStatement(sql);
bundle(pram);
try {
System.out.println("采用PrepareStatement方法执行sql查询语句。。。");
rs=preparedStatement .executeQuery();
}
catch (SQLException e) {
System.out.println("采用PrepareStatement方法执行sql查询语句失败");
e.printStackTrace();
}
return rs;
}
public Boolean updateData(String sql,String[] parm) {
//创建通道
createPsStatement(sql);
//绑定参数
bundle(parm);
int row=0;
try {
System.out.println("修改数据中。。。");
row=preparedStatement.executeUpdate();
} catch (SQLException e) {
System.out.println("修改数据失败。。。");
e.printStackTrace();
}
boolean res=false;
if (row > 0) {
res=true;
}
return res;
}
public void closeAll() {
if (rs !=null) {
try {
System.out.println("关闭resultSet。。。");
rs.close();
} catch (SQLException e) {
System.out.println("关闭resultSet异常。。。");
e.printStackTrace();
}
}
if (stmt !=null) {
try {
System.out.println("关闭statement。。。");
stmt.close();
} catch (SQLException e) {
System.out.println("关闭statement异常。。。");
e.printStackTrace();
}
}
if (preparedStatement !=null) {
try {
System.out.println("关闭preparestatement。。。");
preparedStatement .close();
} catch (SQLException e) {
System.out.println("关闭preparestatement异常。。。");
e.printStackTrace();
}
}
if (con !=null) {
try {
System.out.println("关闭connection。。。");
con.close();
} catch (SQLException e) {
System.out.println("关闭connection异常。。。");
e.printStackTrace();
}
}
}
}
CSS代码:
td {
font-size: 9pt;
color: #000000;
}
a:hover {
font-size: 9pt;
color: #FF0000;
}
a {
font-size: 9pt;
text-decoration: none;
color: #000000;
}
img{
border:0;
}
.img1{
border:1px;
}
.blue {
font-size: 9pt;
color: #034683;
}
.bgcolor {
font-size: 9pt;
color: #1980DB;
}
.btn_grey {
font-family: "宋体";
font-size: 9pt;
color: #333333;
background-color: #eeeeee;
cursor: hand;
padding:1px;
height:19px;
border-top: 1px solid #FFFFFF;
border-right:1px solid #666666;
border-bottom: 1px solid #666666;
border-left: 1px solid #FFFFFF;
}
input{
font-family: "宋体";
font-size: 9pt;
color: #333333;
border: 1px solid #999999;
}
.word_white{
color:#FFFFFF;
}
.word_deepgrey{
color:#999999;
}
.word_orange{
color:#FF6600;
}
.word_green{
color:#FEECEA;
}
.noborder{
border:none;
}
.word_gray{
color:#dddddd;
}
body {
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}
textarea {
font-family: "宋体";
font-size: 9pt;
color: #333333;
border: 1px solid #999999;
}
hr{
border-style:dashed;
color:#CCCCCC;
}
Javascript代码:
function validteCode()
{
//用于存储随机验证码
let codes=new Array(4);
let colors=new Array("Red","Green","Gray","Blue","Maroon","Aqua","Fuchsia","Lime","Olive","Silver");
for(let i=0;i < codes.length;i++)
{
//获取随机验证码
codes[i]=Math.floor(Math.random()*10);
}
let spans=document.getElementById("divCode").all;
for(let i=0;i<spans.length;i++)
{
spans[i].innerHTML=codes[i];
//随机设置验证码颜色
spans[i].style.color=colors[Math.floor(Math.random()*10)];
}
}
document.onload=validteCode();
JSP页面代码:
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>用户登录</title>
<link href="css/style.css" type="text/css" rel="stylesheet">
<script language="javascript">
function reload(){
//document.checkCode.src="PictureCheckCode";
document.getElementById("checkCode").src=document.getElementById("checkCode").src+"?nocache="+new Date().getTime();
}
function checkEmpty(form){
for(let i=0;i<form.length;i++){
if(form.elements[i].value==""){
alert(form.elements[i].title);
return false;
}
}
}
</script>
</head>
<body>
<br>
<table width="766" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="496" valign="top">
<table width="496" height="205" border="0" cellpadding="0"
cellspacing="0" background="images/banne.jpg">
<tr>
<td width="204">
</td>
<td width="278" class="line">
<b>关于工作室</b>
<br>
<br>
工作室是由几个志同道合的朋友一起创办的集博客、论坛、空间、新闻等8大功能于一身的门户网站机构、先已拥有800万固定网友......
<br>
<br>
<a href="register.jsp" class="a3">现在注册 >></a>
</td>
<td width="14">
</td>
</tr>
</table>
<table width="496" border="0" cellpadding="0" cellspacing="0"
background="images/char.jpg">
<tr>
<td height="31" class="word">
</td>
</tr>
</table>
<table width="496" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="55" height="233">
<img src="images/left.jpg" width="55" height="233">
</td>
<td width="10">
</td>
<td width="212" valign="top">
<table width="205" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="45" valign="bottom" class="word">
<span class="lineline"><strong>空间</strong>
<br> 提供50MB免费空间,支持MySQL数据库</span>
</td>
</tr>
</table>
<table width="205" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="54" valign="bottom" class="word">
<span class="lineline"><strong>博客</strong>
<br> 新近研发博客搬家工具,方便之门</span>
</td>
</tr>
</table>
<table width="205" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="58" valign="bottom" class="word">
<span class="lineline"><strong>新闻</strong>
<br> 最新最全的国内外大事全记录</span>
</td>
</tr>
</table>
<table width="205" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="56" valign="bottom" class="word">
<span class="lineline"><strong>文档</strong>
<br> 还在费力的到处查找文档吗,快请进</span>
</td>
</tr>
</table>
</td>
<td width="34">
<img src="images/right.jpg" width="34" height="233">
</td>
<td width="10">
</td>
<td width="175" valign="top">
<table width="168" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="168" height="45" valign="bottom" class="word">
<span class="lineline"><strong>空间</strong>
<br> 30位斑竹热聘中....</span>
</td>
</tr>
</table>
<table width="163" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="163" height="54" valign="bottom" class="word">
<span class="lineline"><strong>下载</strong>
<br> 500万资源供您下载</span>
</td>
</tr>
</table>
<table width="167" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="167" height="58" valign="bottom" class="word">
<span class="lineline"><strong>网摘</strong>
<br> 就像天空的浮云一样</span>
</td>
</tr>
</table>
<table width="166" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="166" height="56" valign="bottom" class="word">
<span class="lineline"><strong>读书</strong>
<br> 读书“武装”自己
</span>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
<td width="270" valign="top">
<table width="237" border="0" align="center" cellpadding="0"
cellspacing="0">
<tr>
<td height="18" align="center">
<a href="#" class="a4">设为首页</a> /
<a href="#" class="a4">加入收藏</a> /
<a href="#" class="a4">联系我们</a>
</td>
</tr>
</table>
<table width="128" border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<img src="images/land.jpg" width="270" height="56">
</td>
</tr>
</table>
<table width="270" height="304" border="0" cellpadding="0"
cellspacing="0">
<tr>
<td background="images/land1.jpg" valign="top">
<%
if (null==session.getAttribute("userInfo")) {
%>
<form name="form" method="post" action="userInfo?method=1"
onSubmit="return checkEmpty(form)">
<table width="231" border="0" align="center">
<tr>
<td width="53" height="25">
登录名:
</td>
<td width="168" height="25">
<input type="text" name="account" title="请输入登录用户名">
</td>
</tr>
<tr>
<td height="25">
密 码:
</td>
<td height="25">
<input type="password" name="password" title="请输入登录密码">
</td>
</tr>
<tr>
<td height="25">
校验码:
</td>
<td height="25">
<input type="text" name="code" title="请输入验证码">
</td>
</tr>
<tr>
<td height="25" colspan="2" align="center" valign="middle">
<table width="185" border="0" cellpadding="0"
cellspacing="0">
<tr>
<td width="69">
<img border=0 src="PictureCheckCode" id="checkCode">
</td>
<td width="116">
<a href="#" onClick="reload();" class="a2">重新获得验证码</a>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td height="25" colspan="2" align="center">
<input name="Submit" type="image" class="noborder" src="images/land.png">
</td>
</tr>
<tr>
<td height="15" colspan="2" align="center">
<a href="register.jsp" class="a2">没有注册?点此处注册</a>
<br>
<br>
<a href="findPS1.jsp" class="a2">如果密码丢失?请找回密码</a>
</td>
</tr>
</table>
</form>
<div align="center">
${requestScope.information}
<%
}
else {
%>
<br>
<br>
</div>
<table width="230" border="0" align="center">
<tr>
<td height="20">用 户 名:</td>
<td>
${sessionScope.userInfo.account}
</td>
</tr>
<tr>
<td height="20">联系邮箱:</td>
<td>
${sessionScope.userInfo.email}
</td>
</tr>
<tr>
<td height="20">
</td>
<td>
<a href="loginOut.jsp" class="a2">重新登录</a>
</td>
</tr>
</table>
<%
}
%>
</td>
</tr>
</table>
<table width="270" height="69" border="0" cellpadding="0"
cellspacing="0" background="images/cidian.jpg">
<tr>
<td width="87">
</td>
<td width="183" class="lineline">
<strong><a href="#" class="a5">编程词典</a>(程序学习工具)</strong>
<br>
</td>
</tr>
</table>
</td>
</tr>
</table>
<table width="766" height="80" border="0" align="center"
cellpadding="0" cellspacing="0" background="images/downLine.jpg">
<tr>
<td align="center">
<font color="#666666">
<br> Copyright? 2008
</font>
</td>
</tr>
</table>
</body>
</html>
register.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="css/style.css" type="text/css" rel="stylesheet">
<title>用户注册</title>
<meta HTTP-EQUIV="Pragma" CONTENT="no-cache">
<meta HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
<meta HTTP-EQUIV="Expires" CONTENT="0">
<script language="javascript">
function checkemail(email){
var str=email;
//在JavaScript中,正则表达式只能使用"/"开头和结束,不能使用双引号
var Expression=/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
var objExp=new RegExp(Expression);
if(objExp.test(str)==true){
return true;
}else{
return false;
}
}
function checkForm(form){
if(form.account.value==""){
window.alert("请输入用户登录昵称!");
return false;
}
if(form.account.value.length < 5 ||form.account.value.length > 20){
window.alert("用户登录昵称必须在5-20之间!");
return false;
}
if(form.password.value==""){
window.alert("请输入用户登录密码!");
return false;
}
if(form.password.value.length < 8){
window.alert("用户登录密码必须在8位以上");
return false;
}
if(form.repassword.value==""){
window.alert("请输入用户登录重复密码!");
return false;
}
if(form.repassword.value!=form.password.value){
window.alert("您输入的两次密码都不正确!");
return false;
}
if(form.email.value==""){
window.alert("请输入email地址");
return false;
}
if(!checkemail(form.email.value)){
window.alert("您输入的email地址格式不正确");
return false;
}
if(form.code.value==""){
window.alert("请输入校验码");
return false;
}
}
function reload(){
document.getElementById("checkCode").src=document.getElementById("checkCode").src+"?nocache="+new Date().getTime();
}
</script>
</head>
<body>
<table width="765" height="120" border="0" align="center" cellpadding="0" cellspacing="0" background="images/re_top.jpg">
<tr>
<td width="520"> </td>
<td width="245" valign="top">
<table width="214" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="252" height="43" valign="bottom">
<a href="#" class="a2">设为首页</a> /
<a href="#" class="a2">加入收藏</a> /
<a href="#" class="a2">联系我们</a>
</td>
</tr>
</table></td>
</tr>
</table>
<table width="765" border="0" align="center" cellpadding="0" cellspacing="0" background="images/re_center.jpg">
<tr>
<td width="538" height="93" align="center">
<form name="form" method="post" action="userInfo?method=2" onSubmit="return checkForm(form)">
<table width="509" height="290" border="0" align="center">
<tr>
<td width="150" height="30" align="right">用户登录昵称:</td>
<td colspan="2">
<input name="account" type="text"> *
</td>
</tr>
<tr>
<td width="150" height="30" align="right"> </td>
<td colspan="2">5-20位字母、数字或字符组合,首字母必须为字母。</td>
</tr>
<tr>
<td height="30" align="right"> </td>
<td colspan="2">为了您的帐户安全,强烈建议你的密码使用字符+数字+特殊字符方式,并且密码的长度大于8位。</td>
</tr>
<tr>
<td height="30" align="right">密码:</td>
<td colspan="2">
<input name="password" type="password"> *
</td>
</tr>
<tr>
<td height="30" align="right">重复输入密码:</td>
<td colspan="2">
<input name="repassword" type="password"> *
</td>
</tr>
<tr>
<td height="30" align="right"> </td>
<td colspan="2">
注意:邮箱必须可以收到狼族工作室激活码的。<br>
用户注册后,需要激活,才可以使用!激活码只会发到你注册邮箱中。
</td>
</tr>
<tr>
<td height="30" align="right">Email地址:</td>
<td colspan="2">
<input name="email" type="text" size="40"> *
</td>
</tr>
<tr>
<td height="30" colspan="3" align="center">
以下两个选项,只要有任何一个没有输入,你将不可能使用通过答案问题新设置密码的功能。
</td>
</tr>
<tr>
<td height="30" align="right"> </td>
<td colspan="2" valign="bottom">用户帮您找回忘记的密码!</td>
</tr>
<tr>
<td height="30" align="right">密码提示问题:</td>
<td colspan="2">
<input name="question" type="text" id="question" size="50">
</td>
</tr>
<tr>
<td height="30" align="right"> </td>
<td colspan="2" valign="bottom">用户帮您找回忘记的密码!</td>
</tr>
<tr>
<td height="30" align="right">提示问题答案:</td>
<td colspan="2">
<input name="result" type="text" id="result" size="50">
</td>
</tr>
<tr>
<td height="30" align="right">校验码:</td>
<td colspan="2">
<img border=0 src="PictureCheckCode" id="checkCode">
</td>
</tr>
<tr>
<td height="30" align="right"> </td>
<td colspan="2">
<a href="#" onClick="reload()">重新获得验证码</a>
</td>
</tr>
<tr>
<td height="30" align="right">输入校验码:</td>
<td colspan="2">
<input name="code" type="text"> *</td>
</tr>
<tr>
<td height="30"> </td>
<td width="307">
<input type="submit" name="Submit" value=" 注 册 用 户 ">
</td>
<td width="83">
<a href="index.jsp" class="a2">返回登录</a>
</td>
</tr>
<tr>
<td height="30"> </td>
<td colspan="2">${requestScope.information}</td>
</tr>
</table>
</form>
</td>
<td width="227">
<table width="174" height="546" border="0" align="center">
<tr>
<td width="164" height="35">会员帮助</td>
</tr>
<tr>
<td><p>A:已注册但未激活的用户</p>
<p><a href="#" class="a6">马上激活我的帐户</a></p></td>
</tr>
<tr>
<td height="68">如果的激活码丢失,你可以:
<a href="#" class="a6">重新通过邮箱获取激活码</a>(发送到你的注册邮箱)
</td>
</tr>
<tr>
<td height="55">如果你的注册邮箱没有接收到激活码,你可以:
<a href="#" class="a6">修改你的注册邮箱</a>(必须知道帐户密码)
</td>
</tr>
<tr>
<td height="30">B:已激活却忘记了密码的用户</td>
</tr>
<tr>
<td height="20"><a href="#" class="a2">通过回答问题新设密码</a></td>
</tr>
<tr>
<td height="20"><a href="#" class="a2">把新设密码的连接发到注册Email</a></td>
</tr>
<tr>
<td height="30">C:已登录用户</td>
</tr>
<tr>
<td height="48">
<a href="#" class="a2">注销登录</a>
<a href="#" class="a2">修改提示问题与答案</a><br> <br>
<a href="#" class="a2">修改注册邮箱</a>
<a href="#" class="a2">新设密码</a>
</td>
</tr>
<tr>
<td height="30">D:注册条款</td>
</tr>
<tr>
<td height="133">
建议意见以及错误反馈请发送邮件到以下邮箱:
<a href="#" class="a2">XXX@163.com</a><br>
需要咨询时候,请务必把你的注册用户名、注册Email、以及其他证明这个账号属于你的信息一起发过来。<br>
有bug反馈的时候,请务必把出现的bug的操作过程,必要的截图一起发过来。
</td>
</tr>
</table>
</td>
</tr>
</table>
<table width="765" height="90" border="0" align="center" cellpadding="0" cellspacing="0" background="images/re_down.jpg">
<tr>
<td align="center">
<font color="#666666">Copyright? 2008 </font>
</td>
</tr>
</table>
</body>
</html>
findPS1.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<html>
<head>
<title>工作室-找回密码第一步</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="0">
<link href="css/style.css" type="text/css" rel="stylesheet">
<script language="javascript">
function checkForm(form){
if(form.account.value==""){
window.alert("请输入用户登录昵称!");
return false;
}
}
</script>
</head>
<body>
<table width="765" height="120" border="0" align="center" cellpadding="0" cellspacing="0" background="images/re_top.jpg">
<tr>
<td width="520"> </td>
<td width="245" valign="top"><table width="214" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="252" height="43" valign="bottom">
<a href="#" class="a2">设为首页</a> /
<a href="#" class="a2">加入收藏</a> /
<a href="#" class="a2">联系我们</a>
</td>
</tr>
</table></td>
</tr>
</table>
<table width="765" border="0" align="center" cellpadding="0" cellspacing="0" background="images/re_center.jpg">
<tr>
<td width="538" height="93" align="center" valign="top">
请输入用户昵称<br>
<br>
<form name="form" method="post" action="userInfo?method=3" onSubmit="return checkForm(form)">
<table width="367" border="0">
<tr>
<td width="99" height="25">用户登录昵称:</td>
<td width="163" colspan="2"><input name="account" type="text"></td>
</tr>
<tr>
<td height="25"> </td>
<td><input type="submit" name="Submit" value=" 提 交 用 户 "></td>
<td><a href="index.jsp" class="a2">返回登录</a></td>
</tr>
<tr>
<td height="30"> </td>
<td colspan="3">${requestScope.information}</td>
</tr>
</table>
</form>
</td>
<td width="227">
<table width="174" height="546" border="0" align="center">
<tr>
<td width="164" height="35">会员帮助</td>
</tr>
<tr>
<td><p>A:已注册但未激活的用户</p>
<p><a href="#" class="a6">马上激活我的帐户</a></p></td>
</tr>
<tr>
<td height="68">如果的激活码丢失,你可以:
<a href="#" class="a6">重新通过邮箱获取激活码</a>(发送到你的注册邮箱)
</td>
</tr>
<tr>
<td height="55">如果你的注册邮箱没有接收到激活码,你可以:
<a href="#" class="a6">修改你的注册邮箱</a>(必须知道帐户密码)
</td>
</tr>
<tr>
<td height="30">B:已激活却忘记了密码的用户</td>
</tr>
<tr>
<td height="20"><a href="#" class="a2">通过回答问题新设密码</a></td>
</tr>
<tr>
<td height="20"><a href="#" class="a2">把新设密码的连接发到注册Email</a></td>
</tr>
<tr>
<td height="30">C:已登录用户</td>
</tr>
<tr>
<td height="48">
<a href="#" class="a2">注销登录</a>
<a href="#" class="a2">修改提示问题与答案</a><br> <br>
<a href="#" class="a2">修改注册邮箱</a>
<a href="#" class="a2">新设密码</a>
</td>
</tr>
<tr>
<td height="30">D:注册条款</td>
</tr>
<tr>
<td height="133">
建议意见以及错误反馈请发送邮件到以下邮箱:<a href="#" class="a2">XXX@163.com</a><br>
需要咨询时候,请务必把你的注册用户名、注册Email、以及其他证明这个账号属于你的信息一起发过来。<br>
有bug反馈的时候,请务必把出现的bug的操作过程,必要的截图一起发过来。
</td>
</tr>
</table>
</td>
</tr>
</table>
<table width="765" height="90" border="0" align="center" cellpadding="0" cellspacing="0" background="images/re_down.jpg">
<tr>
<td align="center">
<font color="#666666">
Copyright? 2008 All Right Reserved!
</font>
</td>
</tr>
</table>
</body>
</html>
findPS2.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<html>
<head>
<title>工作室-找回密码第二步</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="css/style.css" type="text/css" rel="stylesheet">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="0">
<script language="javascript">
function checkForm(form){
if(form.result.value==""){
window.alert("请输入问题答案!");
return false;
}
}
</script>
</head>
<body>
<table width="765" height="120" border="0" align="center" cellpadding="0" cellspacing="0" background="images/re_top.jpg">
<tr>
<td width="520"> </td>
<td width="245" valign="top">
<table width="214" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="252" height="43" valign="bottom">
<a href="#" class="a2">设为首页</a> /
<a href="#" class="a2">加入收藏</a> /
<a href="#" class="a2">联系我们</a>
</td>
</tr>
</table>
</td>
</tr>
</table>
<table width="765" border="0" align="center" cellpadding="0" cellspacing="0" background="images/re_center.jpg">
<tr>
<td width="538" height="93" align="center" valign="top">
请输入问题答案<br>
<br>
<form name="form" method="post" action="userInfo?method=4" onSubmit="return checkForm(form)">
<table width="367" border="0">
<tr>
<td width="99" height="25">问题:</td>
<td width="163" colspan="2">${requestScope.userInfo.question}</td>
</tr>
<tr>
<td width="99" height="25">答案:</td>
<td width="163" colspan="2"><input name="result" type="text">
<input name="account" type="hidden" value="${requestScope.userInfo.account}">
</td>
</tr>
<tr>
<td height="25"> </td>
<td><input type="submit" name="Submit" value=" 提 交 答 案 "></td>
<td><a href="index.jsp" class="a2">返回登录</a></td>
</tr>
<tr>
<td height="30"> </td>
<td colspan="3">${requestScope.information}</td>
</tr>
</table>
</form> </td>
<td width="227">
<table width="174" height="546" border="0" align="center">
<tr>
<td width="164" height="35">会员帮助</td>
</tr>
<tr>
<td><p>A:已注册但未激活的用户</p>
<p><a href="#" class="a6">马上激活我的帐户</a></p></td>
</tr>
<tr>
<td height="68">如果的激活码丢失,你可以:
<a href="#" class="a6">重新通过邮箱获取激活码</a>(发送到你的注册邮箱)
</td>
</tr>
<tr>
<td height="55">如果你的注册邮箱没有接收到激活码,你可以:
<a href="#" class="a6">修改你的注册邮箱</a>(必须知道帐户密码)
</td>
</tr>
<tr>
<td height="30">B:已激活却忘记了密码的用户</td>
</tr>
<tr>
<td height="20"><a href="#" class="a2">通过回答问题新设密码</a></td>
</tr>
<tr>
<td height="20"><a href="#" class="a2">把新设密码的连接发到注册Email</a></td>
</tr>
<tr>
<td height="30">C:已登录用户</td>
</tr>
<tr>
<td height="48">
<a href="#" class="a2">注销登录</a>
<a href="#" class="a2">修改提示问题与答案</a><br> <br>
<a href="#" class="a2">修改注册邮箱</a>
<a href="#" class="a2">新设密码</a>
</td>
</tr>
<tr>
<td height="30">D:注册条款</td>
</tr>
<tr>
<td height="133">
建议意见以及错误反馈请发送邮件到以下邮箱:<a href="#" class="a2">XXX@163.com</a><br>
需要咨询时候,请务必把你的注册用户名、注册Email、以及其他证明这个账号属于你的信息一起发过来。<br>
有bug反馈的时候,请务必把出现的bug的操作过程,必要的截图一起发过来。
</td>
</tr>
</table>
</td>
</tr>
</table>
<table width="765" height="90" border="0" align="center" cellpadding="0" cellspacing="0" background="images/re_down.jpg">
<tr>
<td align="center">
<font color="#666666">
Copyright? 2008 All Right Reserved!
</font>
</td>
</tr>
</table>
</body>
</html>
loginOut.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>退出登录</title>
</head>
<body>
<%
session.invalidate();
response.sendRedirect("index.jsp");
%>
</body>
</html>
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>registerDemo</display-name>
<servlet>
<servlet-name>UserInfoServlet</servlet-name>
<servlet-class>com.demo.servlet.UserInfoServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>PictureCheckCode</servlet-name>
<servlet-class>com.demo.servlet.PictureCheckCode</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserInfoServlet</servlet-name>
<url-pattern>/userInfo</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>PictureCheckCode</servlet-name>
<url-pattern>/PictureCheckCode</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
maven:
avaServer Pages(JSP) 是一种服务器端编程技术,能够为构建基于 Web 的应用程序创建动态的独立于平台的方法。JSP 可以访问 Java API 的整个家族,包括访问企业级数据库的 JDBC API。
本文章帮助初学者们了解 JavaServer Pages(JSP) 的基本功能,从而开发自己的 Web 应用程序。学习后你会发现自己处于一个使用 JSP 专业知识的中等水平,之后你可以达到更高的水平。
搭配视频观看效果更佳~~
Jsp入门到精通
https://www.ixigua.com/6772333813507817987
JSP全称Java Server Pages,是一种动态网页开发技术。它使用JSP标签在HTML网页中插入Java代码。标签通常以<%开头以%>结束。
JSP是一种Java servlet,主要用于实现Java web应用程序的用户界面部分。网页开发者们通过结合HTML代码、XHTML代码、XML元素以及嵌入JSP操作和命令来编写JSP。
JSP通过网页表单获取用户输入数据、访问数据库及其他数据源,然后动态地创建网页。
JSP标签有多种功能,比如访问数据库、记录用户选择信息、访问JavaBeans组件等,还可以在不同的网页中传递控制信息和共享信息。
● 能以模板化的方式简单、高效地添加动态网页内容。
● 可利用JavaBean和标签库技术复用常用的功能代码(设计好的组件容易实现重复利用,减少重复劳动)。标签库不仅带有通用的内置标签(JSTL),而且支持可扩展功能的自定义标签。
●有良好的工具支持。
● 继承了Java语言的相对易用性。
● 继承了Java的跨平台优势,实现“一次编写,处处运行”。因为支持Java及其相关技术的开发平台多,网站开发人员可以选择在最适合自己的系统平台上进行JSP开发;不同环境下开发的JSP项目,在所有客户端上都能顺利访问。
● 页面中的动(控制变动内容的部分)/静(内容不需变动的部分)区域以分散但又有序的形式组合在一起,能使人更直观地看出页面代码的整体结构,也使得设计页面效果和程序逻辑这2部分工作容易分离(外观视图与逻辑分离)。从而方便分配人员并发挥各自长处,实现高效地分工合作。
● 可与其它企业级Java技术相互配合。JSP可以只专门负责页面中的数据呈现,实现分层开发。
一、介绍:
1.JSP是sun公司提供一个规范
2.JSP规范用于封装响应对象简化将Servlet处理结果写入到响应体开发难度
二、响应对象使用时问题
1.手动设置响应头content-type
2.手动索要一个输出流对象
3.手动将结果写入到响应体
三、JSP优势
开发人员你只需考虑哪些内容需要写入到响应体不需要考虑写入到响应包中过程
jsp文件"运行时"自动将文件内部所有与Java命令无关()的内容自动写入到响应体
四、jsp文件中java命令书写规范
1.执行标签
1) 格式: :
<%
java命令行;
%>
2) 作用:用于提示JSP文件在运行时不要将执行标签中Java命令写入到响应体通知JSP文件在运行时需要将执行标签中内容执行完毕后再做输出
3) java命令行:
2.输出标签
1) 格式::
<%=变量名%>
<%=运算表达式%>
2) 作用:
在jsp文件运行时,通知JSP将指定变量内容或则表达式结果写入到响应体
五、jsp文件中内置对象---九
1.ServletContext application
2. HttpSession session
3.HttpServletRequest request
六、Servlet与jsp文件之间关系
1.Servlet: 接收请求并处理请求,但是Servlet不负责将处理结果返回
-----相当于"大厨"
2.JSP:不负责处理请求,负责将Servlet生成的处理结果写入到响应体
-----相当于"传菜员"
3.Servlet与jsp之间调用关系
请求转发
浏览器------>Servlet(处理请求)----------->jsp--->处理结果写入到响应体
4.Servlet与jsp之间如何共享数据借助于request
七、JSP运行原理【面试必考题】
1.JSP文件不是静态资源文件也不是动态资源文件
2.JSP文件不能被编译不能被调用执行
3.运行原理:
1)tomcat接收到调用jsp文件(one.jsp)请求时,tomcat将被访问jsp【编辑】为java文件(one_jsp.java)
2)tomcat调用JVM将java文件【编译】为class文件(one_jsp.class)
3) 这个class文件是一个Servlet接口实现类
4)tomcat通过反射机制生成这个class文件的实例对象
5)tomcat通过实例对象调用_jspService方法,这个方法在运行时负责通过输出流将jsp文件内容写入到响应体
文转自与博客园一杯凉茶的博客
前面讲解了Servlet,了解了Servlet的继承结构,生命周期等,并且在其中的ServletConfig和ServletContext对象有了一些比较详细的了解,但是我们会发现在Servlet中编写一些HTML代码,是很不方便的一件事情,每次都需要out.println(HTML); 因此就出现了JSP,来解决这样的问题,JSP中的内容就是html,但是能够嵌套java语言,现在来详细了解一下JSP。
1、什么是JSP?
JSP(Java Server Pages):是以Java语言为基础的动态网页开发技术,
特点:
Servlet特点:在Java源码中嵌入html源码
JSP特点:在html源码中嵌入java代码
JSP就是Servlet
1、tomcat获得JSP文件后,先将JSP转成servlet,变成xxx.java(servlet源码),
D:\java\tomcat7.0\apache-tomcat-7.0.53\apache-tomcat-7.0.53\work\Catalina\localhost\test01\org\apache\jsp
tomcat安装目录 引擎 主机 项目 固定包名 这个之下就是存放着jsp变成的servlet文件.java和编译文件.class
2、tomcat将java文件编译成class文件
3、tomcat运行class文件,并将结果输出到浏览器,
实例:
创建一个jsp。查看其转换后的servlet代码。
NewFile.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 hahaha 11 </body> 12 </html>
NewFile_jsp.java
1 /* 2 * Generated by the Jasper component of Apache Tomcat 3 * Version: Apache Tomcat/7.0.53 4 * Generated at: 2017-02-22 02:09:08 UTC 5 * Note: The last modified time of this file was set to 6 * the last modified time of the source file after 7 * generation to assist with modification tracking. 8 */ 9 package org.apache.jsp; 10 11 import javax.servlet.*; 12 import javax.servlet.http.*; 13 import javax.servlet.jsp.*; 14 15 public final class NewFile_jsp extends org.apache.jasper.runtime.HttpJspBase 16 implements org.apache.jasper.runtime.JspSourceDependent { 17 18 private static final javax.servlet.jsp.JspFactory _jspxFactory=19 javax.servlet.jsp.JspFactory.getDefaultFactory(); 20 21 private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants; 22 23 private javax.el.ExpressionFactory _el_expressionfactory; 24 private org.apache.tomcat.InstanceManager _jsp_instancemanager; 25 26 public java.util.Map<java.lang.String,java.lang.Long> getDependants() { 27 return _jspx_dependants; 28 } 29 30 public void _jspInit() { 31 _el_expressionfactory=_jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory(); 32 _jsp_instancemanager=org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig()); 33 } 34 35 public void _jspDestroy() { 36 } 37 38 public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response) 39 throws java.io.IOException, javax.servlet.ServletException { 40 41 final javax.servlet.jsp.PageContext pageContext; 42 javax.servlet.http.HttpSession session=null; 43 final javax.servlet.ServletContext application; 44 final javax.servlet.ServletConfig config; 45 javax.servlet.jsp.JspWriter out=null; 46 final java.lang.Object page=this; 47 javax.servlet.jsp.JspWriter _jspx_out=null; 48 javax.servlet.jsp.PageContext _jspx_page_context=null; 49 50 51 try { 52 response.setContentType("text/html; charset=UTF-8"); 53 pageContext=_jspxFactory.getPageContext(this, request, response, 54 null, true, 8192, true); 55 _jspx_page_context=pageContext; 56 application=pageContext.getServletContext(); 57 config=pageContext.getServletConfig(); 58 session=pageContext.getSession(); 59 out=pageContext.getOut(); 60 _jspx_out=out; 61 62 out.write("\r\n"); 63 out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\r\n"); 64 out.write("<html>\r\n"); 65 out.write("<head>\r\n"); 66 out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\r\n"); 67 out.write("<title>Insert title here</title>\r\n"); 68 out.write("</head>\r\n"); 69 out.write("<body>\r\n"); 70 out.write("\thahaha\r\n"); 71 out.write("</body>\r\n"); 72 out.write("</html>"); 73 } catch (java.lang.Throwable t) { 74 if (!(t instanceof javax.servlet.jsp.SkipPageException)){ 75 out=_jspx_out; 76 if (out !=null && out.getBufferSize() !=0) 77 try { out.clearBuffer(); } catch (java.io.IOException e) {} 78 if (_jspx_page_context !=null) _jspx_page_context.handlePageException(t); 79 else throw new ServletException(t); 80 } 81 } finally { 82 _jspxFactory.releasePageContext(_jspx_page_context); 83 } 84 } 85 }
可以看到public final class NewFile_jsp extends org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent
NewFile_jsp.java继承自HttpJspBase。来看看HttpJspBase的源码
HttpJspBase.java
1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.apache.jasper.runtime; 19 20 import java.io.IOException; 21 22 import javax.servlet.ServletConfig; 23 import javax.servlet.ServletException; 24 import javax.servlet.http.HttpServlet; 25 import javax.servlet.http.HttpServletRequest; 26 import javax.servlet.http.HttpServletResponse; 27 import javax.servlet.jsp.HttpJspPage; 28 29 import org.apache.jasper.compiler.Localizer; 30 31 /** 32 * This is the super class of all JSP-generated servlets. 33 * 34 * @author Anil K. Vijendran 35 */ 36 public abstract class HttpJspBase extends HttpServlet implements HttpJspPage { 37 38 private static final long serialVersionUID=1L; 39 40 protected HttpJspBase() { 41 } 42 43 @Override 44 public final void init(ServletConfig config) 45 throws ServletException 46 { 47 super.init(config); 48 jspInit(); 49 _jspInit(); 50 } 51 52 @Override 53 public String getServletInfo() { 54 return Localizer.getMessage("jsp.engine.info"); 55 } 56 57 @Override 58 public final void destroy() { 59 jspDestroy(); 60 _jspDestroy(); 61 } 62 63 /** 64 * Entry point into service. 65 */ 66 @Override 67 public final void service(HttpServletRequest request, HttpServletResponse response) 68 throws ServletException, IOException 69 { 70 _jspService(request, response); 71 } 72 73 @Override 74 public void jspInit() { 75 } 76 77 public void _jspInit() { 78 } 79 80 @Override 81 public void jspDestroy() { 82 } 83 84 protected void _jspDestroy() { 85 } 86 87 @Override 88 public abstract void _jspService(HttpServletRequest request, 89 HttpServletResponse response) 90 throws ServletException, IOException; 91 }
public abstract class HttpJspBase extends HttpServlet implements HttpJspPage
看到了一个熟悉的类,HttpServlet,我们编写Servlet时就是继承自该类,这里也是继承HttpServlet,并且HttpJspBase的源码会发现,生命周期也是有init()方法,service()方法,destory()方法,相当于_jspService()方法就是servlet的service()方法的执行,所以说JSP也是一个servlet。
我们在JSP写的所有html代码,都会被转换为servlet中的out.write(html)代码来输出。看图
小总结:
对于jsp转换成的servlet源码,之后我们会在进行详细分析,现在只需要知道jsp中的内容在servlet中被转换成什么了,在哪里被转换了即可。其中_jspService()方法的详细内容下面会讲解
注意:jsp 生成java源码,默认第一次生成,之后直接执行,除非内容修改,具体点说,由于JSP只会在客户端第一次请求的时候被编译,因此第一次请求JSP时会感觉比较慢,而之后的请求因为不会编译JSP,所以速度就快多了,如果将Tomcat保存的JSP编译后的class文件删除,Tomcat也会重新编译JSP。在开发Web程序的时候经常需要修改JSP,Tomcat能够自动检测到JSP程序的改动,如果检测到JSP源代码发生了改动,Tomcat会在下次客户端请求JSP时重新编译JSP,而不需要重启Tomcat,这种自动检测功能默认是开启的,检测改动会消耗少量的时间,在部署web应用程序的时候可以在web.xml中将它关掉。这也就是为什么我们能够在jsp页面直接修改内容,而不用重新启动服务器的原因。
因为JSP就是servlet,那么生命周期也就是跟serlvet一样。
JSP和servlet有一点区别就在于:jsp是先部署后编译,而servlet是先编译后部署。
JSP模版数据:
就是JSP中的HTML代码,它的内容给是固定的,无论程序如何运行模版数据输出到客户端浏览器时都不会发生改变,当我们创建一个JSP时,模版就已经固定了。
元素:JSP中的java部分,包括脚本(JavaScript,或者java代码)以及JSP指令(Directive)与JSP标签(Tag)等,元素决定着程序的流程,元素是不会显示到浏览器的。这几个都会在接下来讲解到
JSP脚本:
1、使用<% 编写java代码 %>,中间java代码必须遵循Java语法,
为什么能够使用out输出?这里就涉及到了JSP的九大内置对象了,后面会讲解到,到时候回过头来在看看这里,就会知道为什么可以使用。
来看看,jsp变为servlet时的代码是如何编写的。
在JSP中JSP指令(后面会讲):
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
这句代码在servlet就变为了
response.setContentType("text/html; charset=UTF-8");//这句代码的意思就是通知tomcat和浏览器都使用UTF-8码表,并且发送回浏览器的数据类型是text/html。这是有JSP指令中画红色部分代码转变的,而指令中的pageEncoding="UTF-8"的意思是JSP转换为Servlet时采用UTF-8码表编码,因为可能JSP中包含中文。
对于JSP模版数据来说,就原封不动的使用out.write()来输出到浏览器
而对于JSP元素来说,就会变成普通的java代码,因为在servlet中,就可以直接编写java代码。
2、使用<%=xxx %>来输出结果
使用<%=result %>来输出结果,servlet中就会将其转换为out.print(result)进行输出。输出各种类型数据:int、double、boolean、String、Object等
3、JSP注释
<%-- --%> :jsp注释,
// :java单行注释
/* */ :Java多行注释
<!-- --> :这个注释,会发送到浏览器端的源码中显示
注释分别在servlet中如何显示的?
JSP注释不会在servlet文件中显示,而java注释则会,但其所有的注释到了浏览器端,都不会出现在源码中,只有<!-- -->这个注释会到浏览器的网页源码中去
4、JSP中申明方法与属性(全局变量) 使用<%! 方法、属性%>
这个就不演示了,就是在JSP中编写方法或者属性时,使用<%! %>括起来。
5、在JSP中使用if语句,或者使用for循环,whilt循环等都可以实现,也就是编写脚本而已。
指令用来申明JSP页面的一些属性,比如编码方式,文档类型。我们在servlet中也会申明我们使用的编码方式和响应的文档类型的,而JSP就是用指令来申明。上面我们也说到了一条指令,也就是page指令,
JSP指令格式:<%@ directive {attribute=value}* %>
解释:
directive:指令名称,例如page指令
attribute=value:紧跟指令名称后面的就是各种属性,以键值对的形式书写
*:代表后面能跟0个或多个属性。
例如: page指令:用来声明JSP页面的属性等。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> page指令,后面跟着三个属性,分别是language、contentType、pageEncoding。这只是其中的几个属性,并没有写全,page指令允许的属性如下表所示
属性名称 取值范围 描述
language java 解释该JSP文件时采用的语言,一般为java语言,默认为java
extends 任何类的全名 编译该JSP文件时继承哪个类,JSP为Servlet,因此当指明继承普通类时需要实现Servlet的init、destroy等方法
import 任何包名、类名 引入该JSP中用到的类、包等,import是唯一可以声明多次的page指令属性,一个import可以引用uogelei,中间用英文逗号隔开,
如<%@ page import="java.util.List,java.util.ArrayList"%>
session true、false 该JSP内是否内置Session对象,如果为true,则内置Session对象,可直接使用,否则反之,默认为true
autoFlush true,false 是否运行缓存,如果为true,则使用out.println()等方法输出的字符串并不是立刻到达客户端服务器的,而是暂时存到缓存里,缓存满
了或者程序执行完毕或者执行out.flush()操作时才到客户端,默认为true。
buffer none或者数字KB 指定缓存大小,当autoFlush设为true时有效,例如<%@ page buffer=10kb%>
isThreadSafe true,false 是否线程安全,如果为true,则运行多个线程同时运行该jsp程序,否则只运行一个线程,其余线程等待,默认为false
isErrorPage true,false 指定该页面是否为错误显示页面,如果为true,则该JSP内置有一个Exception对象exception,可直接使用,否则没有,默认为false
errorPage 某个JSP页面的相对路径 指明一个错误页面,如果该JSP程序抛出一个未捕捉的异常,则转到errorPage指定的页面,errorPage指定的页面通常
isErrorPage属性为true,且内置的exception对象为未捕捉的异常
contentType 有效的文档类型 客户端浏览器根据该属性判断文档类型,例如 HTML格式为text/html、纯文本格式为text/plain、JPG图像为image/jpeg、GIF图 像为image/gif、WORD文档为application/msword,该属性常跟着charset设置编码一起,作用是通知服务器和浏览器都使用同一 个码表
info 任意字符串 指明JSP的信息,该信息可以通过Servlet.getServletInfo()方法获取到
trimDirective Whitespaces true、false 是否去掉指令前后的空白字符,默认为false
pageEncoding UTF-8,ISO-8859-1等 指定一张码表来对该JSP页面进行编码,
include指令
比较简单,只有一种形式 <%@ include file="relativeURL"%> relativeURL:本应用程序内另一个JSP文件或者HTML文件的路径,例如,网址内所有页面均有一个统一风格的导航栏和页脚版权,那么就可以使用该指令将其包含进来,
特点:include指令会将包含页面的源代码添加到使用include指令的页面中来,然后编译成class文件,而等下会讲到的一个JSP行为,<jsp:include page="relativeURL">作用跟include指令一样,但是不同的是,include行为是运行时单独执行包含页面,然后把执行的结果包含到本页面来,属于先运行后包含。
taglib指令
JSP支持标签技术,后面会讲到标签的用法,jstl标签库的使用等,
作用:用来指明JSP页面内使用的JSP标签库,taglib指令有两个属性,uri为类库的地址,prefix为标签的前缀
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
前面讲了JSP语法,介绍了JSP页面中的内容有哪些,分别有什么作用,就两个东西,模块数据和元素。其中元素有包括脚本,指令,标签,脚本就是JSP中嵌入java代码,指令作用就是申明页面的属性,那标签是干嘛的,标签分为JSP自带内置的标签,和通过taglib指令来使用JSP标签库,或者自定义标签。现在我们先来讲一些JSP内置的标签。
JSP内置的标签就被称为JSP行为(JSP Actions)。只要书写很少的标记代码就能使用JSP提供的丰富功能,JSP行为其实是对常用的JSP功能的抽象与封装,可以取代jsp脚本,让JSP中就少一些嵌入java代码的地方。
格式:<jsp:elements {attribute="value"}* />
jsp:标签的前缀,说明是jsp内置的标签,
elements:行为的名称,
attribute=value:使用键值对来编写属性
*:能指定0个或多个属性对
<jsp:include />行为
include行为用于运行时包含某个文件,如果被包含的文件为JSP程序,则先会执行JSP程序,然后在把执行的结果包含进来。 作用是跟include指令一样的,唯一的区别就在于,include指令是将被包含的文件的源码加入到了本JSP程序中,然后在进行编译,属于静态包含,而include行为只是将被包含的文件的运行结果包含进自己。属于动态包含
Java bean行为
是一组与Java Bean 相关的行为,包括useBean行为、setProperty行为、getProperty行为等
Java Bean就是普通的Java类,也被称为POJO,只有私有的属性与对应的getter方法和setter方法,注意其中当私有的属性为boolean类型时,习惯上一般把getter方法写成isXxx();而不是getXxx();
useBean行为
<jsp:useBean id="beanObject" class="className" scope="Value"> 作用:在jsp中定义一个java bean对象,
id:指明Java Bean对象的名称,JSP中可以使用该名称引用该Java Bean对象,相当于给new出来的对象取一个变量名,
class:Java Bean类的全名
scope:该java bean对象的作用范围,可以写的就四个,也就是JSP的四大作用域,page、request、session、application
page:只能在当前JSP页面使用,如果不在JSP页面,那么就会失效
request:这个前面学过,A页面请求转发到B页面,那么使用的是同一个request,那么A,B页面都算是request的作用域,也就是通过请求转发的页面都是其作 用域
session:该作用域在一个web项目下任何位置应该读访问的到,只要cookie不关闭,并且cookie设置 的访问路径为"/",
application:其实就是Servlet中的servletContext,服务器下的所有项目都能访问到。
setProperty行为
<jsp:setProperty name="beanName" property="propertyName" value="">
对Java Bean对象进行属性的设置
name:java bean对象的名称,也就是在useBean行为中的id
property:对象中的属性名,
value:要对其属性进行赋值的值
getProperty行为
<jsp:getProperty name="beanName" property="propertyName" />
获取JavaBean对象的某个属性值
name:java bean 对象的名称,也就是在useBean行为中的id
property:对象的属性名
例子: javabean:User.java NewFile.jsp
User.java
1 package a; 2 3 public class User { 4 private int id; 5 private String username; 6 private String password; 7 public int getId() { 8 return id; 9 } 10 public void setId(int id) { 11 this.id=id; 12 } 13 public String getUsername() { 14 return username; 15 } 16 public void setUsername(String username) { 17 this.username=username; 18 } 19 public String getPassword() { 20 return password; 21 } 22 public void setPassword(String password) { 23 this.password=password; 24 } 25 26 27 }
NewFile.jsp
1 <body> 2 <!-- 创建一个新的javabean对象user,会先判断在page作用域内是否有叫user对象的javabean,如果有则取它,如果没有则创建新的javabean对象 --> 3 <jsp:useBean id="user" class="a.User" scope="page"></jsp:useBean> 4 <!-- 对javabean对象的username进行赋值 --> 5 <jsp:setProperty property="username" name="user" value="wu"/> 6 <!-- 获取javabean对象的username属性 --> 7 <jsp:getProperty property="username" name="user"/> 8 </body>
可以查看NewFile.jsp变为servlet后的源代码,看看我们写的javabean行为会被转换为何种语句
这里出现了一个JSP九大内置对象中的一个,pageContext。现在简单提一句,pageContext就是JSP页面的管理者(上下文),其中的getAttribute(name,scope)方法是获取指定作用域中的数据的,如果getAttribute(name)方法的话,默认是对page作用域进行操作,findAttribute(name)依次从page、request、session、application获得内容。
在第一个红框中,就代表中我们的useBean行为,其中进行了一次判断,就是如果在page作用域中找不到user这个对象,那么就创建一个新的,否则就使用找到的这个user对象,
第二个红框中,代表着我们的setProperty行为,先找到user对象,然后在对其属性进行赋值
第三个红框中,代表着我们的getProperty行为,也是先找到user对象,然后在获取其属性的值。
注意:对于javabean行为来说,有一个特点的地方,就是当请求过来的参数对应javabean的属性时,可以为其一次性设置所有的值
<jsp:setProperty name="user" property="*" /> //设置user的所有属性,属性值从request中自动取得,*代表所有属性。
<jsp:forward />行为
实现请求转发功能,Servlet中通过request.getRequestDispatcher("someServlet").forward(request,response);而在JSP中也能够实现相同的功能,只不过用的是<jsp:forward />行为,实际上forward行为就是对其进行了封装。
格式:
<jsp:forward page="someServlet">
<jsp:param name="param1" value="value1"/>
<jsp:param name="param2" value="value2"/>
</jsp:forward>
page:需要跳转到的页面或者servlet、 <jsp:param/>参数行为,带一些参数过去,name、value是以键值对的形式带过去的
例如:
NewFile.jsp
MyServlet.java
访问:http://localhost:8080/test01/NewFile.jsp
浏览器地址栏没有改变,说明是请求转发
NewFile_jsp.java
使用return的好处是执行完上面的转发,就直接return,没有必要在执行下面的代码了,对参数使用了URLEncode进行编码,说明该<jsp:param/>可以直接传递中文,但是前提是要设置request.setCharacterEncoding("UTF-8");为什么这样做,看上面框起来中的代码。
<jsp:directive/>行为
directive行为,就相当于JSP指令,比如<jsp:directive.page/>相当于<%@ page %>指令,等等其它指令是一样的书写格式。
之前那么一大段内容,就是为了讲解什么是JSP,JSP中的内容是怎么样的,到这里应该知道的差不多了, 但我们还要深入了解一些东西,我们知道JSP中的内容就只有两种,模版数据,和元素,元素就包括了指令呀,脚本呀,标签(行为)呀,脚本会慢慢被标签全部代替,也就是说JSP中基本上不会嵌入Java代码,但是我们也知道JSP会转换为servlet,在Servlet中,输出数据时,都需要通过response.getWrite();但是在JSP中,直接使用out对象进行输出,为什么呢?这就是因为out为JSP的一个隐藏对象,JSP中内置了9个隐藏对象,使得JSP比Servlet使用起来更简单,更方便,
page、config、application、request、response、session、out、exception、pageContext
page:page对象代表当前JSP页面,是当前JSP编译后的Servlet类的对象。相当于this。
config:标识Servlet配置,类型:ServletConfig,api跟Servlet中的ServletConfig对象是一样的,能获取该servlet的一些配置信息,能够获取ServletContext
application:标识web应用上下文,类型:ServletContext,详情就看Servlet中的ServletContext的使用
request:请求对象, 类型:httpServletRequest
response:响应对象 类型:httpServletResponse
session:表示一次会话,在服务器端记录用户状信息的技术
out:输出响应体 类型:JspWriter
exception 表示发生异常对象,类型 Throwable,在上面我们介绍page指令中的一个errorPage属性时就有说到他
pageContext:表示 jsp页面上下文(jsp管理者) 类型:PageContext
标记了红色的对象就是JSP独有的,其他的都是Servlet中的老东西。
在这个由jsp转换为servlet的文件中,只能看到8个内置对象,少了exception对象,因为我们在将page指令时,说过一个isErrorPage属性,默认是false,被关闭了,所以其中并没有exception对象。
JSP的四大作用域:page、request、session、application
这四大作用域,其实就是其九大内置对象中的四个,为什么说他们也是JSP的四大作用域呢?因为这四个对象都能存储数据,比如request.setAttribute()注意和request.setParameter()区分开来,一个是存储在域中的、一个是请求参数,session.setAttribute()、application其实就是SerlvetContext,自然也有setAttribute()方法。而page作用域的操作就需要依靠pageContext对象来进行了。在上面我们也有提到JSP的四大作用域,
page作用域:代表变量只能在当前页面上生效
request:代表变量能在一次请求中生效,一次请求可能包含一个页面,也可能包含多个页面,比如页面A请求转发到页面B
session:代表变量能在一次会话中生效,基本上就是能在web项目下都有效,session的使用也跟cookie有很大的关系。一般来说,只要浏览器不关闭,cookie就会一直生效,cookie生效,session的使用就不会受到影响。
application:代表变量能一个应用下(多个会话),在服务器下的多个项目之间都能够使用。比如baidu、wenku等共享帐号。
out对象:
类型:JspWriter
jsp 输出底层使用 response.getWriter();什么意思呢?这里就要讲解一下JSP缓存和Servlet缓存了,输出的过程是这样的
JSP页面转换为Servlet后,使用的out对象是JspWriter类型的,所以是会先将要发送的数据存入JSP输出缓存中,然后,等JSP输出缓存满了在自动刷新到servlet输出缓存,等serlvet输出缓存满了,或者程序结束了,就会将其输出到浏览器上。除非手动out.flush()。
验证servlet输出缓存和JSP输出缓存和我们上面所说的是正确的。
分析:如果按没有jsp缓存和servlet缓存的话,输出的结果应该是aaaabbbbcccc,但是输出的却是bbbbaaaacccc,为什么呢?按照我们上面所说的原理进行分析,out对象是先将其输出到JSP缓存中,所以aaaa加入了jsp缓存,而response.getWriter().print("bbbb")是直接将bbbb输出到servlet缓存中,然后又使用out对象将cccc输出到jsp缓存,到程序结束,servlet缓存中有bbbb,然后jsp会将缓存中的内容就刷新到servlet缓存中,serlvet就是bbbbaaaacccc了,然后到浏览器也就得到我们的输出结果了。如果在12行将注释去掉,那么输出的结果又会是什么呢?答案就是aaaabbbbcccc,过程自行分析。
pageContext对象:重点
这个功能就比较强大了,比较牛逼,基本上什么他都有,因为是它是JSP页面的管理者(上下文),所以JSP中的内置对象呀,它统统能够获得,下面介绍它的api。
1、获得其它八大内置对象 getXxx()
pageContext.getOut(); //获得out对象
pageContext.getApplication(); //获得application对象
等等....
2、对作用域的属性进行操作(四大作用域)
对默认作用域的属性进行操作。page
pageContext.getAttribute(name); //获得page作用域数据
pageContext.setAttribute(name,value); //给page作用域设置内容
pageContext.removeAttribute(name); //给page作用域移除内容
3、对指定作用域的属性进行操作
getAttribute(name,scope); //获得 指定作用域中的数据
setAttribute(name,value); //给指定作用域设置内容
removeAttribute(name ,scope) 移除指定作用域的内容(page/request/session/application)
4、提供作用域常量
PageContext.PAGE_SCOPE page
PageContext.REQUEST_SCOPE request
PageContext.SESSION_SCOPE response
PageContext.APPLICATION_SCOPE application
5、一次获得指定名称内容
findAttribute(name); //依次从page、request、session、application 获得内容
response对象:
就是响应对象,、如果不了解就看看讲解request和response的这一章节的内容
config对象:
类型:ServletConfig
能够获取servlet的初始化参数,获取servletContext对象,获取servletName
api详情请看讲解servlet这一章节
exception异常对象:
包含了异常的信息
使用它,必须结合page指令中的isErrorPage属性和errorPage属性。
exception.jsp 抛异常的一个NullPointException,并且跳转到error.jsp错误显示页面 errorPage属性的意思是如果发生未捕捉到的异常,将会跳转到error.jsp页面
error.jsp isErrorPage属性说明该页面是一个错误显示页面,则可以使用exception对象
访问http://localhost:8080/test01/exception.jsp
通过上面一大堆一大堆的学习,我们应该知道这些东西
1、什么是JSP?
JSP本质上就是一个servlet,因为servlet输出html太麻烦了,所以就有了JSP,JSP就是专门用来书写html的,当然其中也能写java代码。
2、JSP的内容包括什么?
模版数据和元素。其中元素有包括脚本(java代码)、指令(页面属性)、和行为(标签,为了JSP中不嵌入那么多java代码衍生的)
3、JSP中九大内置对象是哪九个?
九大内置对象,page、config、appliction、request、response、session、out、exception、pageContext
4、九大内置对象和servlet中对象的关系
page就是jsp转换为servletservlet对象本身,也就是this
config -- Servlet中的servletConfig
application -- Servlet中的ServletContext
request -- Servlet中的request
response -- Servlet中的response
session -- Servlet中的session
out -- JspWriter
exception -- 异常对象
pageContext -- 表示 jsp页面上下文(jsp管理者) 类型:PageContext,
其中pageContext对象最牛逼,有了他就拥有了天下,哈哈~
5、JSP中的四大作用域。
page、request、session、application
其中操作page域中属性需要借助pageContext对象。
6、JSP中还有其他两大块内容
一个是EL表达式,很重要,
另一个是jstl标签库的使用,也很重要,在接下来的两节中,就会讲解到。
*请认真填写需求信息,我们会在24小时内与您取得联系。