MIME
数据库连接
<!%
public static final String DBDRIVER=”oracle.jdbc.driver.OracleDriver”;
public static final String DBURL=”jdbc
racle:thin@localhost:1521
xxx”;
public static final String BDUSER=””;
public static final String BDPW=””;
%>
<%
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
%>
<%
Class.forName(DBDRIVER);
conn= DriverManager.getConnection(DBURL,DBUSER,DBPW);
String sql=””;
pstmt=conn.prepareStatement(sql);
rs=pstmt.executeQuery();
%>
<%
rs.close();
pstmt.close();
conn.close();
%>
include
<jsp:include /> 或者<%@include file=””%>
包含页面, @include是静态包含, 只包含内容.
<jsp:include />是动态包含, 如果是jsp文件,则显示结果.如果不是如@include.
<jsp:include page=”” />
或者给包含页面传递参数
<jsp: include page=””>
<jsp: param name=“” value=”” />
</jsp: include>
被包含的页面通过<%=request.getParameter(“”)%>获得参数.
forward 无条件跳转, 服务器端跳转, 客户端的URL不变. 当然可以传参. 参数<%=val %>
<jsp:forward page=””/>
或者带参数
<jsp:forward page=””>
<jsp:param name=”“ vlaue=”“/>
</jsp:forward>
登录程式
1. 数据库脚本
DROP TABLE testUser;
CREATE TABLE testUser(
uid NUMMBER PRIMARY KEY NOT NULL,
username VARCHAR2(10) NOT NULL,
password VARCHAR2(10) NOT NULL
);
INSERT INTO testUser(uid,username,password) VALUES (xxx,'xxx','xxx');
commit;
2. login.jsp
<%@page contentType=”text/html; charset=utf-8”%>
<head>
<script language=”javaScript”>
function validation(f){
if(!(/^\w(5,15)$/.test(f.username.value))){
alert('the length of username must be between 5 und 15 characters');
f.username.focus();
return false;
}
if(!(/^\w(5,15)$/.test(f.password.value))){
alert('the length of password must be between 5 und 15 characters');
f.username.focus();
return false;
}
return true;
}
</script>
</head>
<body>
<form action=”check.jsp” methode=”post”onSubmit=”validation(this);”>
<table border=”0”>
<tr>
<td colspan =”2”> Login Here<td/>
</tr>
<tr>
<td> USER ID: </td>
<td><input type=”text” name=”username”></td>
</tr>
<tr>
<td>PASSWORD:</td>
<td><input type=”password” name=”password”></td>
</tr>
<tr colspan=”2”>
<td><input type=”submit” value=”submit”></td>
<td><input type=”reset” value=”reset”></td>
</tr>
</table>
</body>
3. check.jsp
<%@page import=”java.sql.*”%>
<%!
public static final String DBDRIVER = “orcale.jdbc.driver.OrcaleDriver”;
public static final String DBURL=”jdbc
rcale:thin:@localhost:1521:XE”;
public static final String DBUSER=””;
public static final String DBPASS=””;
%>
<%
Connection conn=null;
preparedStatement ps=null;
ResaultSet rs=null;
boolean loginFlag=false;
%>
<%
String username = request.getParameter(“username”);
String password= request.getParameter(“password”);
try{
Class.forName(DBDRIVER);
conn=DriverManager.getConnection(DBURL,DBUSER,DBPASS);
String sql= “SELECT username, password FROM testUSER WHERE username=? AND password=?”;
ps=conn.prepareStatement(sql);
ps.setString(1,username); //把username给sql中的第一个问号
ps.setString(2,password);
rs=ps.executeQuery();
if(rs.next()){
loginFlag= true;
}
}catch(Exception e){
}finally{
try{
conn.close();
}catch(Exception e){}
}
%>
<%
if(loginFlag){
%>
<jsp:forward page=”success.jsp”/>
<%
}else{
%>
<jsp:forward page=”failure.jsp”/>
<%
}
%>
4. success.jsp
<%@page contentType=”text/html; charset=utf-8”%>
<h1>welcome</>
5. failure.jsp
<%@page contentType=”text/html;charset=utf-8”%>
<h1>failure, <a href=”login.jsp”>turn back</a></h1>
内置对象.
在使用内置对象时, 不需要通过构造方法实例化就能使用.
| No. | Objekt | Type |
| 1 | pageContext | javax.servlet.jsp.PageContext |
| 2 | request | javax.servlet.http.HttpServletRequest |
| 3 | response | javax.servlet.http.HttpServletResponse |
| 4 | session | javax.servlet.http.HttpSession |
| 5 | application | javax.servlet.ServletContext |
| 6 | config | javax.servlet.ServletConfig |
1. page
不能跳转
但在 pageContext.setAttribute(name, value, pageContext.REQUEST_SCOPE);
来改变属性的范围. pageContext.REQUEST_SCOPE; pageContext.SESSION_SCOPE;
pageContext.APPLICATION_SCOPE, 默认不写为pageContext.
2.request
可以forward, 但是不能通过link跳转. 在服务器端可以多次跳转.
request.setCharacterEncoding(“utf-8”);转码
如果是复选框传参, 需要使用public String[] getParameterValues(String name);
String.startsWith(String str, int offset) 布尔函数. 用于判断字符串开头匹配
String getRemoteAddr(); 取得客户端ip.
3.session
只针对一个用户, 无跳转限制
4. application
所有用户.
5. response 服务器端的回应.
| No. | Methods | Desc |
| 1 | setHeader(String name, String value) | 设置http头信息名字和内容 |
| 2 | sendRedirect(String location) throws IOException | 跳转页面 |
| 3 | addCookie(Cookie cookie) | 向客户端加入Cookie |
| 4 | setContentType(String type) | 设置内容返回类型MIME |
例子:取得头信息
<%@ page contentType=”text/html; charset=utf-8”%>
<%@ page import = “java.util.*”%>
<%
Enumeration enu= request.getHeaderNames();
while(enu.hasMoreElements()){
String name=(String)enu.nextElement();
%>
<%=name%> –> <%=request.getHeader(name)%>
<%
}
%>
应用: 定时刷新或者定时跳转, 使用”refresh”关键字
<%@ page contentType=”text/html; charset=utf-8”%>
<%
response.setHeader(“refresh”, “2;URL=xxx.jsp”);
%>
以上代码表示2秒跳转到xxx.jsp. 如果是
response.setHeader(“refresh”, “2”); 则表示定时刷新本页面.
不过如果是静态页,可以直接在页面头信息写入
<META HTTP-EQUIV=”refresh” CONTENT=”2;URL=xxx.jsp”>
以上两种方法等价, 后者更快. 都属于客户端跳转.
如果想要累加刷新. 那么 变量就要是静态变量. 比如用<%! %>声明
跳转指令(客户端跳转)
response.sendRedirect(“xxx.jsp”);
这种跳转会在页面内容执行后才会执行. 而forward属于无条件跳转, 只要执行到,就立即跳转. 所以使用jdbc时, conn.close()一定要在forward之前. 而对于sendRedirect则无所谓.
Cookie的使用
Cookie是指服务器存储在客户端的一些信息. 通过http头信息传递. 如要设置Cookie就要使用
<%@ page contentType=”text/html; charset=utf-8”%>
<%
Cookie c1 = new Cookie(“username”,”LDS”);
Cookie c2 = new Cookie(“password”,”LDS”);
response.addCookie(c1);
response.addCookie(c2);
c1.setMaxAge(20); //设置Cookie存活时间, 默认只存在浏览器打开时
c2.setMaxAge(50);
%>
如果要想取得Cookie, 则要使用request中的 public Cookie[] getCookies();
<%@ page contentType=”text/html; charset=utf-8”%>
<%
Cookie c[] = request.getCookies();
for(int i=0; i<c.length; i++){
%>
<%=c[i].getName()%>-----><%=c[i].getValue()%>
<%
}
%>