1.JSP
实例1:在网页上显示当前时间
index.html
<%@page import="java.text.SimpleDateFormat"%>
<%@ page language="java" import="java.util.*" contentType="text/html" pageEncoding="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%>">
<title>My JSP 'index.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>
<% SimpleDateFormat format=new SimpleDateFormat("yyy年MM月dd日 hh时mm分ss秒");
String time=format.format(new Date());
%>
<%=time %>
</body>
</html>
实例2:实现登录功能,并捕获登录信息。
login.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+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<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">
-->
</head>
<body>
<form action="dologin.jsp" method="post">
用户名:<input type="text" name="username" /><br>
密码:<input type="password" name="password" /><br>
兴趣爱好:<input type="checkbox" name="hobby" value="足球"/>足球
<input type="checkbox" name="hobby" value="排球"/>排球
<input type="checkbox" name="hobby" value="篮球"/>篮球<br>
<input type="submit" value="立即登录">
</form>
</body>
</html>
dologin.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+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'dologi.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>
<%
request.setCharacterEncoding("utf-8");
String user=request.getParameter("username");
String pwd=request.getParameter("password");
String[] hobby=request.getParameterValues("hobby");
for(int i=0;i<hobby.length;i++){
out.println(hobby[i]);
}
%>
<%="用户名:"+user+" "+"密码:"+pwd %>
</body>
</html>
2.JDBC数据库查询
实例:使用JDBC和SQL语句查询数据库信息
目录结构:
数据库信息:
DBUtil.java
package com.zrgj.shanji.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* 数据库连接
* @author Administrator
*
*/
public class DBUtil {
// 导包
public DBUtil(){
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
// 创建连接
public Connection getConn(){
Connection conn=null;
try {
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=UTF-8","root","root");
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
// 关闭资源
public static void closeAll(ResultSet rs,PreparedStatement pstmt,Connection conn){
try {
if (rs!=null) {
rs.close();
}
if(pstmt!=null){
pstmt.close();
}
if (conn!=null) {
conn.close();
}
}catch (SQLException e) {
e.printStackTrace();
}
}
}
StudentDao.java
package com.zrgj.shanji.dao;
import com.zrgj.shanji.entity.Student;
public interface StudentDao {
public Student getStudent(String studynum);
}
StudentDaoImpl.java
package com.zrgj.shanji.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.zrgj.shanji.dao.StudentDao;
import com.zrgj.shanji.entity.Student;
import com.zrgj.shanji.util.DBUtil;
public class StudentDaoImpl implements StudentDao {
DBUtil db=new DBUtil();
private Connection conn=null;
private PreparedStatement pstmt=null;
private ResultSet rs=null;
public Student getStudent(String studynum) {
Student stu=null;
String sql="select * from student where studynum="+"'"+studynum+"'";
conn=db.getConn();
try {
pstmt=conn.prepareStatement(sql);
rs=pstmt.executeQuery();
while(rs.next()){
stu=new Student();
int sid=rs.getInt("sid");
String sname=rs.getString("sname");
String spwd=rs.getString("spwd");
String gender=rs.getString("gender");
stu.setSid(sid);
stu.setSname(sname);
stu.setSpwd(spwd);
stu.setGender(gender);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return stu;
}
}
Student.java
package com.zrgj.shanji.entity;
public class Student {
private int sid;
private String sname;
private String spwd;
private String gender;
private String studynumString;
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getSpwd() {
return spwd;
}
public void setSpwd(String spwd) {
this.spwd = spwd;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getStudynumString() {
return studynumString;
}
public void setStudynumString(String studynumString) {
this.studynumString = studynumString;
}
}
StudentTest.java
package test;
import com.zrgj.shanji.dao.StudentDao;
import com.zrgj.shanji.dao.impl.StudentDaoImpl;
import com.zrgj.shanji.entity.Student;
public class StudentTest {
public static void main(String[] args) {
StudentDao stuDao=new StudentDaoImpl();
Student stu=stuDao.getStudent("119120110");
System.out.println(stu.getSname());
}
}