学习JSP、Servlet、JDBC
用Java开发Web应用程序时用到的技术主要有两种,即Servlet和JSP。 Servlet是在服务器端执行的Java程序,一个被称为Servlet容器的程序(其实就是服务器) 负责执行Java程序。而JSP(Java Server Page)则是一个页面, 由JSP容器负责执行。
Servlet和JSP两者最大的区别就是,Servlet以Java程序为主, 输出HTML代码时需要使用out.println函数,也就是说Java中内嵌HTML; 而JSP则以HTML页面为主,需要写Java代码时则在页面中直接插入Java代码, 即HTML中内嵌Java。JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口组成。JDBC提供了一种基准,据此可以构建更高级的工具和接口,使数据库开发人员能够编写数据库应用程序。
JSP+Servlet+JDBC的小例子:
其中login.jsp如下:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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>
<center>Welcome</center>
<center>
<form action="login" method="post">
userName<input type="text" name="username" /><br><br>
passWord<input type="text" name="password" /><br><br> <br>
<input type="submit" value="submit">
</form>
</center>
</body>
</html>
其中loginServlet如下:
package servlet;
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javabean.DButil;
public class LoginServlet implements javax.servlet.Servlet{
public void destroy() {
}
public ServletConfig getServletConfig() {
return null;
}
public String getServletInfo() {
return null;
}
public void init(ServletConfig arg0) throws ServletException {
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
String userName = request.getParameter("username");//取得用户名
String password = request.getParameter("password");//取得密码
DButil db = new DButil();//构建数据库对象
boolean canLogin = db.loginSuccess(userName, password);
if(canLogin){//根据登陆情况,跳转页面
response.sendRedirect("welcome.jsp");
}else{
response.sendRedirect("back.jsp");
}
}
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
HttpServletRequest rq = (HttpServletRequest)request;
HttpServletResponse rs = (HttpServletResponse) response;
doPost(rq,rs);
}
}
成功返回:
失败返回:
其中DButil如下:
package javabean;
import java.sql.*;
public class DButil {
boolean bInited = false;
//加载驱动
public void initJDBC() throws ClassNotFoundException {
//加载MYSQL JDBC驱动程序
Class.forName("com.mysql.jdbc.Driver");
bInited = true;
System.out.println("Success loading Mysql Driver!");
}
public Connection getConnection() throws ClassNotFoundException,SQLException{
if(!bInited){
initJDBC();
}
//连接URL为 jdbc:mysql//服务器地址/数据库名
//后面的2个参数分别是登陆用户名和密码
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test","root","root");
return conn;
}
public boolean loginSuccess(String username,String password){
boolean returnValue = false;
String sql = "SELECT * FROM student";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try{
conn = getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while(rs.next()){
String userNameInDB = rs.getString("uname");
String passwordInDB = rs.getString("passwd");
if(userNameInDB.equals(username) &&
passwordInDB.equals(password)){
returnValue = true;
break;
}
}
}catch (ClassNotFoundException e) {
e.printStackTrace();
}catch (SQLException e) {
e.printStackTrace();
}
return returnValue;
}
}
该贴由koei123转至本版2015-7-14 11:05:48