急编写一个简单JSP登陆,明天交各位帮忙。。
发布网友
发布时间:2022-04-09 04:18
我来回答
共3个回答
热心网友
时间:2022-04-09 05:48
不知道这个对你有没有用。
将一个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;
}
}
如果这样不可以的话!我也没有话说了
热心网友
时间:2022-04-09 07:06
<%@ 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>
数据库的读取和判断写得有点简单.
热心网友
时间:2022-04-09 08:40
唉。。。我不懂编程啊。。我想学。。你们能教教我吗。我也前学过VB。JAVA。。不过现在忘得差不多了。。我的QQ是:466359001