【JSP学习笔记】1jsp入门
标签快捷键 shift ctrl < > 注释 shift ctrl /
XML
xml一般操作懂
特殊符号处理
w3school
注释:选中文本然后快捷键ctrl shift c
dtd 存放约束语法
JSP
eclipse视图切换
index.jsp会默认当成首页
<%@page import="java.util.Date"%><!-- 导入java包 引用多个包 采用 ,隔开-->
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
欢迎你
</body>
<%
System.out.println(new Date());//输出到eclipse控制台
out.println(new Date());//输出到客户端
%>
</html>
<% <-这里加-- 就会起注释作用 没有的话就不会注释
//--%> <-为上面直接加--做了准备
<%-- --%> 、/**/ 、// 、注释
<%!
int x = 2; 在<%!%>内定义的是全局变量 定义在class 里面
%>
<%%>是局部变量 在方法里面定义
内容输出表达式 <%= %>
http传输按照协议格式的数据包
http协议规定了我们在发起http请求的时候,这个请求的数据包里面都包含了什么样的数据以及数据按照什么样的先后顺序存放到数据包里面
http学习https://blog.csdn.net/chen1403876161/article/details/51546653
网页分析(采用Google浏览器才可以查看)
form data 里面有我们密码
status code查看连接效果
通过下面代码进行数据包传递
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println(username+password);
内置对象
九个 request respond Session out session application config pageContext page exception
request.getParameter("XXX");获得客户端发送过来的
response 给服务器一些响应
生命周期
test1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
out.print("跳转");
request.setAttribute("message", "来了老弟");
request.getRequestDispatcher("test2.jsp").forward(request, response);
%>
<a href="test2.jsp">s</a>
</body>
</html>
test2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
Object s = request.getAttribute("message");
%>
<%=s %>
</body>
</html>
test1 的数据发送给 test2 test2没接收到 显示null
原因:test1向服务器访问请求后 服务器会生成一个request respond对象 访问test2又会生成一个request对象 导致request respond为null
解决:请求转发!!代码已经改了
注册界面
equest.getRequestDispatcher(url).forward(request,response)是采用请求转发方式,在跳转页面的时候是带着原来页面的request和response跳转的,request对象始终存在,不会重新创建。转发数据不会丢失,forward 就是转寄的意思,就是把客户端第一次请求在服务器内部完成,带着之前的请求request 和服务器解析成response 一并转发过去。说的通俗点就是之前的所有数据。
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="log.jsp">登陆</a>
<a href="register.jsp">注册</a>
</body>
</html>
log.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
Object s =request.getAttribute("message");
%>
<%=s %>
<form action="log_do.jsp" method="post">
用户名:<input type="text" name="username">
密码: <input type="password" name="password">
<input type="submit" value="login">
</form>
</body>
</html>
log_do.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="com.jsp.db"%>
<%@page import="com.jsp.user"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String username = new String(request.getParameter("username").getBytes("iso-8859-1"),"utf-8");
String password = request.getParameter("password");
user u = db.chickuser(username, password);
if(u!=null){
out.print("登陆成功");
request.setAttribute("user", u);
request.getRequestDispatcher("success.jsp").forward(request, response);
}
else {
request.setAttribute("message", "登录失败重新登陆");
request.getRequestDispatcher("log.jsp").forward(request, response);
out.print("登陆失败");
}
%>
</body>
</html>
register.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="register_do.jsp" method="post">
用户名:<input type="text" name="username"/><br>
密码: <input type="password" name="password"/><br>
专业: <input type="text" name="major"><br>
性别: 男<input type="radio" name="sex" value="1"/> 女<input type="radio" name="sex" value="0"/><br>
<input type="submit" value="注册">
</form>
</body>
</html>
register_do.jsp
<%@page import="com.jsp.db"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
</body>
</html>
<%
String username =new String(request.getParameter("username").getBytes("iso-8859-1"),"utf-8");//按照字符串传递
String password =request.getParameter("password"); //接受发过来的信息
String major =request.getParameter("major");
String sex = request.getParameter("sex");
boolean issuccess = db.add(username, password, major, sex);//判断是否传参保存成功
if(issuccess){
//out.print("<font color ='green'>注册成功</font>");
%>
<font color ="green">注册成功</font>
<script type="text/javascript" language="javascript">
alert("注册成功");
</script>
<% request.setAttribute("message", "注册成功请登陆"); //注册成功后跳转到其他界面
request.getRequestDispatcher("log.jsp").forward(request, response);
}
else {
request.setAttribute("message", "注册失败请登陆"); //注册成功后跳转到其他界面
request.getRequestDispatcher("log.jsp").forward(request, response); //ctrl + shift c 注释
//out.print("<font color ='red'>注册失败</font>"); //out.print可以内嵌标签
%>
<font color ="red">注册失败</font>
<%
}
%>
success.java
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="com.jsp.user"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
user u = (user)request.getAttribute("user"); //类型转化
%>
用户名:<%=u.getUsername() %>
密码:<%=u.getPassword() %>
</body>
</html>
user.java
package com.jsp;
public class user {
private String username; // 按照字符串传递
private String password;
private String major;
private String sex;
public user(String username, String password, String major, String sex) {
super();
this.username = username;
this.password = password;
this.major = major;
this.sex = sex;
}
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;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "user [username=" + username + ", password=" + password + ", major=" + major + ", sex=" + sex + "]";
}
}