功能描述:
1、login登录,转至action处理
2、action检查用户是否合法,如合法新建会话变量表示用户成功登录并转至user,否则转至login
3、user显示欢迎界面和详细成绩
一般的JSP程序设计步骤:
1、数据准备
执行脚本,新建SQL Server数据库:
CREATE DATABASE SMIS
GO
USE SMIS
CREATE TABLE stu
(
number varchar(6) primary key, --学号
name varchar(8) not null, --姓名
password varchar(6) not null, --密码
)
GO
INSERT stu VALUES ('000001','黎明','888888')
INSERT stu VALUES ('000002','赵怡春','888888')
INSERT stu VALUES ('000003','张富平','888888')
INSERT stu VALUES ('000004','白丽','888888')
INSERT stu VALUES ('000005','牛玉德','888888')
INSERT stu VALUES ('000006','姚华','888888')
INSERT stu VALUES ('000007','李南','888888')
GO
CREATE TABLE grade(
number varchar (6) not NULL ,
course varchar (50) not NULL ,
grade int not NULL
CONSTRAINT PK_grade PRIMARY KEY CLUSTERED
(
number,course
) ON [PRIMARY]
) ON [PRIMARY]
GO
insert grade (number,course,grade) values ( '000001','C',90)
insert grade (number,course,grade) values ( '000001','Java',56)
insert grade (number,course,grade) values ( '000002','Java',74)
insert grade (number,course,grade) values ( '000002','English',86)
insert grade (number,course,grade) values ( '000003','Database',67)
insert grade (number,course,grade) values ( '000003','C',80)
insert grade (number,course,grade) values ( '000003','Java',89)
insert grade (number,course,grade) values ( '000004','C',77)
insert grade (number,course,grade) values ( '000004','Java',76)
insert grade (number,course,grade) values ( '000004','English',90)
insert grade (number,course,grade) values ( '000005','OS',91)
配置ODBC数据源:StuWeb
2、login.jsp
<%@page c%>
<html>
<head>
</head>
<body>
<form method="POST" action="action.jsp">
<p>用户名称:<input type="text" name="username" size="20"></p>
<p>密 码:<input type="password" name="password" size="20"></p>
<input type="submit" value="登录" name="submit">
</form>
</body>
</html>
3、action.jsp
<%@page import="java.sql.*" %>
<%@page c%>
<%
String username=request.getParameter("username");
String password=request.getParameter("password");
Connection con=null;
Statement stm=null;
ResultSet res=null;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:StuWeb");
stm=con.createStatement();
res=stm.executeQuery("select count(*) from stu where name='"+new String(username.getBytes("ISO8859_1"))+"' and password='"+password+"'");
res.next();
if(res.getInt(1)==1)
{
session.removeAttribute("user");
session.setAttribute("user",username);
RequestDispatcher dispatcher = request.getRequestDispatcher("user.jsp");
dispatcher.forward(request, response);
}
else
{
RequestDispatcher dispatcher = request.getRequestDispatcher("login.jsp");
dispatcher.forward(request, response);
}
}
catch(Exception ex)
{
out.print(ex.getMessage());
}
finally
{
con.close();
}
%>
4、user.jsp
<%@page import="java.sql.*" %>
<%@page c%>
<%
String username=(String)session.getAttribute("user");
out.print("Welcome!"+ new String(username.getBytes("ISO8859_1"))+"<br>");
Connection con=null;
Statement stm=null;
ResultSet res=null;
ResultSetMetaData rsmd=null;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:StuWeb");
stm=con.createStatement();
res=stm.executeQuery("select course,grade from stu inner join grade on stu.number=grade.number where name='"+new String(username.getBytes("ISO8859_1"))+"'");
rsmd = res.getMetaData();
while (res.next())
{
for(int i=1;i<=rsmd.getColumnCount();i++)
{
out.print(res.getObject(i)+" ");
}
out.print("<br>");
}
}
catch(Exception ex)
{
}
finally
{
con.close();
}
%>
基于MVC结构的JSP应用程序
1、JavaBean
package stuApp;
import java.sql.*;
import java.util.*;
public class StuInfo
{
Connection con=null;
Statement stm=null;
public StuInfo()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:StuWeb");
stm=con.createStatement();
}
catch(Exception ex)
{
System.out.print(ex.getMessage());
}
}
public boolean isValid(String username,String password)
{
ResultSet res=null;
boolean result=false;
try
{
res=stm.executeQuery("select count(*) from stu where name='"+username+"' and password='"+password+"'");
res.next();
if(res.getInt(1)==1)
{
result=true;
}
else
{
result=false;
}
}
catch(Exception ex)
{
System.out.print(ex.getMessage());
}
return result;
}
public Vector getGrade(String username)
{
ResultSet res=null;
ResultSetMetaData rsmd;
Vector result=new Vector();
try
{
res=stm.executeQuery("select course,grade from stu inner join grade on stu.number=grade.number where name='" + username + "'");
rsmd = res.getMetaData();
while (res.next())
{
Vector newone=new Vector();
for(int i=1;i<=rsmd.getColumnCount();i++)
{
newone.add(res.getString(i));
}
result.add(newone);
}
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}
return result;
}
public void finalize()
{
try
{
if(con!= null)
con.close();
}
catch(SQLException sqle)
{
System.out.println("Unable to close database connection.");
}
}
public static void main(String [] args)
{
StuInfo si=new StuInfo();
System.out.println(si.isValid("tom","tom"));
Vector result=si.getGrade("黎明");
for(int i=0;i<result.size();i++)
{
Vector subResult=(Vector)result.get(i);
for(int j=0;j<subResult.size();j++)
System.out.println(subResult.get(j));
}
}
}
2、login.jsp
<%@page c%>
<jsp:useBean id="si" scope="session" class="stuApp.StuInfo"/>
<html>
<head>
</head>
<body>
<form method="POST" action="action.jsp">
<p>用户名称:<input type="text" name="username" size="20"></p>
<p>密 码:<input type="password" name="password" size="20"></p>
<input type="submit" value="登录" name="submit">
</form>
</body>
</html>
3、action.jsp
<%@page c%>
<%
String username=request.getParameter("username");
String password=request.getParameter("password");
stuApp.StuInfo si=(stuApp.StuInfo)session.getAttribute("si");
if(si.isValid(new String(username.getBytes("ISO8859_1")),password))
{
session.removeAttribute("user");
session.setAttribute("user",username);
RequestDispatcher dispatcher = request.getRequestDispatcher("user.jsp");
dispatcher.forward(request, response);
}
else
{
RequestDispatcher dispatcher = request.getRequestDispatcher("login.jsp");
dispatcher.forward(request, response);
}
%>
4、user.jsp
<%@page c%>
<%@page import="java.util.*" %>
<%
String username=(String)session.getAttribute("user");
username=new String(username.getBytes("ISO8859_1"));
out.print("Welcome!"+ username +"<br>");
stuApp.StuInfo si=(stuApp.StuInfo)session.getAttribute("si");
Vector result=si.getGrade(username);
for(int i=0;i<result.size();i++)
{
Vector subResult=(Vector)result.get(i);
for(int j=0;j<subResult.size();j++)
out.println(subResult.get(j));
out.print("<br>");
}
%>
还可以使用Dreamweaver网页设计工具美化界面
[此贴子已经被作者于2010-12-12 08:20:21编辑过]