关于JSP,程序设计。。。跪求代码。。在线等着中…………
题目要求:已知数据库(student),有user,表结构为username:用户名:password:密码:都是随便写,里边没有要求要求:第一个网页:Login.jsp...
题目要求:
已知数据库(student) ,有user,表结构为
user name: 用户名:
password: 密码:
都是随便写,里边没有要求
要求:第一个网页:Login.jsp ,上有表
用户登录
用户名:
密码:
登录 取消
用户输入用户名。密码后,点登录按钮,查询数据库。如果登录成功后跳转到成功界面(success.jsp),否则跳转到失败界面(failure.jsp)
success.jsp和failure.jsp 这两个可以只显示登录成功和登录失败
还有就是和数据库连接的代码,谢谢各位了。 展开
已知数据库(student) ,有user,表结构为
user name: 用户名:
password: 密码:
都是随便写,里边没有要求
要求:第一个网页:Login.jsp ,上有表
用户登录
用户名:
密码:
登录 取消
用户输入用户名。密码后,点登录按钮,查询数据库。如果登录成功后跳转到成功界面(success.jsp),否则跳转到失败界面(failure.jsp)
success.jsp和failure.jsp 这两个可以只显示登录成功和登录失败
还有就是和数据库连接的代码,谢谢各位了。 展开
展开全部
我这个是jsp+servlet+MySQL做出来的不知道对你的要求合不合适!
呵呵 要是不合适你可以在线咪下我!!呵呵
1.先连接数据库!我不知道你是什么数据库我的是MySQL的我先我的吧!!
package com.softeem.connection;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBConnection {
private static final String URL="jdbc:mysql://localhost:3306/user";
private static final String NAME="root";
private static final String PASSWORD="root";
private static final String DRIVERCLASS="com.mysql.jdbc.Driver";
static{
try {
Class.forName(DRIVERCLASS);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException{
return DriverManager.getConnection(URL, NAME, PASSWORD);
}
public static void close(ResultSet rs,PreparedStatement pstat,Connection conn) throws SQLException{
if(rs!=null){
rs.close();
}if(pstat!=null){
pstat.close();
}if(conn!=null){
conn.close();
}
}
}
2.建个DAO吧!这样会比较的清晰点!!
package com.softeem.dao;
import java.util.List;
import com.softeem.dto.Student;
public interface IStudentDAO {
public Student findByName(String name,String password);
3.DAO实现类impl
package com.softeem.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.softeem.connection.DBConnection;
import com.softeem.dao.IStudentDAO;
import com.softeem.dto.Student;
public class StudentDAOImpl implements IStudentDAO {
public Student findByName(String name,String password) {
Connection conn=null;
PreparedStatement pstat=null;
ResultSet rs=null;
String sql="select student_name,student_password from t_student where student_name=?";
try {
conn=DBConnection.getConnection();
pstat=conn.prepareStatement(sql);
pstat.setString(1, name);
//pstat.setString(2, password);
rs=pstat.executeQuery();
Student student=new Student();
if(rs.next()){
student.setStudentName(rs.getString("student_name"));
student.setStudentPassword(rs.getString("student_password"));
}
return student;
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
DBConnection.close(rs, pstat, conn);
} catch (SQLException e) {
e.printStackTrace();
}
}
return null;
}
4.servlet 中!!
package com.softeem.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.softeem.dao.IStudentDAO;
import com.softeem.dto.Student;
import com.softeem.impl.StudentDAOImpl;
public class LoginServlet extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
private IStudentDAO studentDAO=new StudentDAOImpl();
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("gbk");
String name=request.getParameter("name");
String password=request.getParameter("pass");
Student student=studentDAO.findByName(name, password);
if(name.equals(student.getStudentName()) && password.equals(student.getStudentPassword())){
request.getRequestDispatcher("/success.jsp").forward(request, response);
}else{
request.getRequestDispatcher("/failure.jsp").forward(request, response);
}
}
}
5.jsp中的代码!!
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>login.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=gbk">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<form action="http://localhost/student_manager/LoginServlet" method="post">
<table>
<tr><td>用户名:<input type="text" name="name"></td></tr>
<tr><td>密 码:<input type="password" name="pass"></td></tr>
</td></tr><input type="submit" value="登陆"/></td></tr>
</table>
</form>
</body>
</html>
呵呵 代码可能有点多 不过我觉得这样做思路会比较的清晰点 还有许多的简单方法可以做!!希望你能够采纳 呵呵!!
其中数据库和数据库中俄字段和你的不一样你自己改下应该就可以了!!!
还要写有一个类与数据库中数据的相匹配的!!那个很简单的自己写吧!!
呵呵 要是不合适你可以在线咪下我!!呵呵
1.先连接数据库!我不知道你是什么数据库我的是MySQL的我先我的吧!!
package com.softeem.connection;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBConnection {
private static final String URL="jdbc:mysql://localhost:3306/user";
private static final String NAME="root";
private static final String PASSWORD="root";
private static final String DRIVERCLASS="com.mysql.jdbc.Driver";
static{
try {
Class.forName(DRIVERCLASS);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException{
return DriverManager.getConnection(URL, NAME, PASSWORD);
}
public static void close(ResultSet rs,PreparedStatement pstat,Connection conn) throws SQLException{
if(rs!=null){
rs.close();
}if(pstat!=null){
pstat.close();
}if(conn!=null){
conn.close();
}
}
}
2.建个DAO吧!这样会比较的清晰点!!
package com.softeem.dao;
import java.util.List;
import com.softeem.dto.Student;
public interface IStudentDAO {
public Student findByName(String name,String password);
3.DAO实现类impl
package com.softeem.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.softeem.connection.DBConnection;
import com.softeem.dao.IStudentDAO;
import com.softeem.dto.Student;
public class StudentDAOImpl implements IStudentDAO {
public Student findByName(String name,String password) {
Connection conn=null;
PreparedStatement pstat=null;
ResultSet rs=null;
String sql="select student_name,student_password from t_student where student_name=?";
try {
conn=DBConnection.getConnection();
pstat=conn.prepareStatement(sql);
pstat.setString(1, name);
//pstat.setString(2, password);
rs=pstat.executeQuery();
Student student=new Student();
if(rs.next()){
student.setStudentName(rs.getString("student_name"));
student.setStudentPassword(rs.getString("student_password"));
}
return student;
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
DBConnection.close(rs, pstat, conn);
} catch (SQLException e) {
e.printStackTrace();
}
}
return null;
}
4.servlet 中!!
package com.softeem.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.softeem.dao.IStudentDAO;
import com.softeem.dto.Student;
import com.softeem.impl.StudentDAOImpl;
public class LoginServlet extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
private IStudentDAO studentDAO=new StudentDAOImpl();
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("gbk");
String name=request.getParameter("name");
String password=request.getParameter("pass");
Student student=studentDAO.findByName(name, password);
if(name.equals(student.getStudentName()) && password.equals(student.getStudentPassword())){
request.getRequestDispatcher("/success.jsp").forward(request, response);
}else{
request.getRequestDispatcher("/failure.jsp").forward(request, response);
}
}
}
5.jsp中的代码!!
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>login.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=gbk">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<form action="http://localhost/student_manager/LoginServlet" method="post">
<table>
<tr><td>用户名:<input type="text" name="name"></td></tr>
<tr><td>密 码:<input type="password" name="pass"></td></tr>
</td></tr><input type="submit" value="登陆"/></td></tr>
</table>
</form>
</body>
</html>
呵呵 代码可能有点多 不过我觉得这样做思路会比较的清晰点 还有许多的简单方法可以做!!希望你能够采纳 呵呵!!
其中数据库和数据库中俄字段和你的不一样你自己改下应该就可以了!!!
还要写有一个类与数据库中数据的相匹配的!!那个很简单的自己写吧!!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询