急编写一个简单JSP登陆,明天交各位帮忙。。
编写一个简单的JSP程序,要求用户输入的用户名和密码,然后进行数据库验证,不论成功与否,都应当在当前JSP页上输出结果实在做不出来了,明天就交了,各位帮忙啊。。谢谢忘说了...
编写一个简单的JSP程序,要求用户输入的用户名和密码,然后进行数据库验证,不论成功与否,都应当在当前JSP页上输出结果
实在做不出来了,明天就交了,各位帮忙啊。。谢谢
忘说了,连接SQL数据库,您只要写大概就行,要是不写JSP调用JSP能实现吗,就是说只有1个JSP实现所有功能
汗汗~暴瀑汗1楼大哥您别开玩笑 展开
实在做不出来了,明天就交了,各位帮忙啊。。谢谢
忘说了,连接SQL数据库,您只要写大概就行,要是不写JSP调用JSP能实现吗,就是说只有1个JSP实现所有功能
汗汗~暴瀑汗1楼大哥您别开玩笑 展开
3个回答
展开全部
不知道这个对你有没有用。
将一个JSP表单的name和password提交到一个servlet(如CheckLogin ,在test包中),具体代码如下:
package test;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class CheckLogin extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
String name=request.getParameter("name");
String pswd=request.getParameter("password");
String sql="select sname,spassword from fly";
Connection conn=test.getConnection();
Statement stmt=conn.createStatement();
ReslutSet rs=stmt.executeQuery(sql);
String sname = rs.getString("sname");
String spassword = rs.getString("spassword");
if (name.equals(smane) && pswd.equals(spassword)){
response.sendRedirect("success.jsp");
}
else{
response.sendRedirect("error.jsp");
}
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
public void destroy() {
}
}
连接数据库的类DBConnection 例如也放在test包下
package test;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.*;
public class DBConnection {
public static Connection getConnection() {
String driver =驱动连接串;
String url =驱动连接url;
String name =数据库用户名;
String password = 该用户的密码;
Connection conn = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url , name, password);
}
catch (SQLException ex1) {
ex1.printStackTrace();
}
catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
return conn;
}
}
如果这样不可以的话!我也没有话说了
将一个JSP表单的name和password提交到一个servlet(如CheckLogin ,在test包中),具体代码如下:
package test;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class CheckLogin extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
String name=request.getParameter("name");
String pswd=request.getParameter("password");
String sql="select sname,spassword from fly";
Connection conn=test.getConnection();
Statement stmt=conn.createStatement();
ReslutSet rs=stmt.executeQuery(sql);
String sname = rs.getString("sname");
String spassword = rs.getString("spassword");
if (name.equals(smane) && pswd.equals(spassword)){
response.sendRedirect("success.jsp");
}
else{
response.sendRedirect("error.jsp");
}
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
public void destroy() {
}
}
连接数据库的类DBConnection 例如也放在test包下
package test;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.*;
public class DBConnection {
public static Connection getConnection() {
String driver =驱动连接串;
String url =驱动连接url;
String name =数据库用户名;
String password = 该用户的密码;
Connection conn = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url , name, password);
}
catch (SQLException ex1) {
ex1.printStackTrace();
}
catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
return conn;
}
}
如果这样不可以的话!我也没有话说了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%
String msg = "";
boolean flag = false;
String username ;
String password ;
if(request.getParameter("username")!=null){
flag = true;
username = request.getParameter("username");
pass = request.getParameter("pass");
}
if(flag){
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";
//pubs为你的数据库的
String user="sa";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select username ,password from test where name = "+username;
ResultSet rs=stmt.executeQuery(sql);
if(rs.next()){
if(pass.equals(rs.getString("password"))){
esg = "成功登录";
}else{
esg = "用户名或者密码错误";
}
}
}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<table width="211" border="0" align="center">
<tr>
<td>用户名:</td>
<td> <input type="text" name="username" size="20"/></td>
</tr>
<tr>
<td>密码:</td>
<td> <input type="password" name="pass" size="20" /></td>
</tr>
<tr>
<td colspan="2" align="center"> <%=msg%></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="确定" /><input type="reset" value="重设" /></td>
</tr>
</table>
</form>
</body>
</html>
数据库的读取和判断写得有点简单.
<%
String msg = "";
boolean flag = false;
String username ;
String password ;
if(request.getParameter("username")!=null){
flag = true;
username = request.getParameter("username");
pass = request.getParameter("pass");
}
if(flag){
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";
//pubs为你的数据库的
String user="sa";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select username ,password from test where name = "+username;
ResultSet rs=stmt.executeQuery(sql);
if(rs.next()){
if(pass.equals(rs.getString("password"))){
esg = "成功登录";
}else{
esg = "用户名或者密码错误";
}
}
}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<table width="211" border="0" align="center">
<tr>
<td>用户名:</td>
<td> <input type="text" name="username" size="20"/></td>
</tr>
<tr>
<td>密码:</td>
<td> <input type="password" name="pass" size="20" /></td>
</tr>
<tr>
<td colspan="2" align="center"> <%=msg%></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="确定" /><input type="reset" value="重设" /></td>
</tr>
</table>
</form>
</body>
</html>
数据库的读取和判断写得有点简单.
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
唉。。。我不懂编程啊。。我想学。。你们能教教我吗。我也前学过VB。JAVA。。不过现在忘得差不多了。。我的QQ是:466359001
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询