从网上看了一个例子 但是没找到他的用户名和密码是在哪里编辑的 高手帮看看
登录页面:login.html 
<!-- 该Login页面是一个简单的登录界面 -->
<!--
 该JSP程序是用来测试与MySQL数据库的连接,
 需要一个数据库:LearnJSP,和其中一个表:userinfo
 表中有两个字段分别为:UserName varchar (20) not null,UserPwd varchar (20) not null
-->
<html>
  <head>
    <title>登录</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <meta http-equiv="Content-Language" content="ch-cn">
  </head>
  <body>
 <!-- Form 用来提取用户填入并提交的信息-->
 <form method="post" name="frmLogin" action="LoginServlet">
   <h1 align="center">用户登录</h1><br>
   <div align="center">用户名:
      <input type="text" name="txtUserName" value="Your name"
       size="20" maxlength="20"
       onfocus="if(this.value=='Your name')this.value='';"><br>密码:
      <input type="password" name="txtPassword" value="Your password"
       size="20" maxlength="20"
       onfocus="if(this.value=='Your password')this.value='';"><br>
      <input type="submit" name="Submit" value="提交" onClick="validateLogin();" >
       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      <input type="reset" name="Reset" value="重置"><br>
   </div>
 </form>
 <!-- javaScript 函数 validateLogin(),用来验证用户名和密码是否为空 -->
    <script language="javaScript">
     function validateLogin()
     {
      var sUserName = document.frmLogin.txtUserName.value;
      var sPassword = document.frmLogin.txtPassword.value;
      if( sUserName=="" )
      {
       alert("请输入用户名!");
       return false;
      }
      if( sPassword=="" )
      {
       alert("请输入密码!");
       return false;
      }
     }
    </script>
  </body>
</html> 登录成功欢迎页面:login_success.jsp 
<%@ page language="java" contentType="text/html; charset=UTF-8"
 pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
  <title>My JSP 'login_failure.jsp' starting page</title>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <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>
  <%
   String userName = (String)session.getAttribute ( "UserName" );
  %>
  <div align=center>
   <%=userName%>
   欢迎您,登录成功!
  </div>
 </body>
</html> 登录失败页面:login_failure.jsp<%@ page language="java" contentType="text/html; charset=UTF-8"
 pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
  <title>My JSP 'login_failure.jsp' starting page</title>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <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>
  <%
  String userName = (String)session.getAttribute ( "UserName" );
  %>
  <div align=center>
   <%=userName%>
   对不起,登录失败!
  </div>
 </body>
</html> Servlet处理文件:LoginServlet.java 
/**
 * 该JSP程序是用来测试与MySQL数据库的连接,
 * 需要一个数据库:LearnJSP,和其中一个表:userinfo
 * 表中有两个字段分别为:UserName varchar (20) not null,UserPwd varchar (20) not null
 */
package zieckey.login.servlet;
import java.sql.Statement;
import java.io.IOException;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet implements Servlet
{
 public LoginServlet ()
 {
  // TODO Auto-generated constructor stub }
 /*
  * (non-Javadoc)
  *
  * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
  * javax.servlet.http.HttpServletResponse)
  */
 @Override
 protected void doGet ( HttpServletRequest arg0, HttpServletResponse arg1 )
   throws ServletException, IOException
 {
 }
 /*
  * (non-Javadoc)
  *
  * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
  * javax.servlet.http.HttpServletResponse)
  */
 @Override
 protected void doPost ( HttpServletRequest request, HttpServletResponse response )
   throws ServletException, IOException
 {
  response.setContentType ( "text/html" );
  String result = "";
  // 获取用户名  String sUserName = request.getParameter ( "txtUserName" );
  if ( sUserName == "" || sUserName == null || sUserName.length ( ) > 20 )
  {
   try
   {
    result = "请输入用户名(不超过20字符)!";
    request.setAttribute ( "ErrorUserName", result );
    response.sendRedirect ( "login.html" );
   } catch ( Exception e )
   {
   }
  }
  // 获取密码  String sPasswd = request.getParameter ( "txtPassword" );
  if ( sPasswd == "" || sPasswd == null || sPasswd.length ( ) > 20 )
  {
   try
   {
    result = "请输入密码(不超过20字符)!";
    request.setAttribute ( "ErrorPassword", result );
    response.sendRedirect ( "login.html" );
   } catch ( Exception e )
   {
   }
  }
  // 登记JDBC驱动程序  try
  {
   Class.forName ( "org.gjt.mm.mysql.Driver" ).newInstance ( );
  } catch ( InstantiationException e )
  {
   // TODO Auto-generated catch block   e.printStackTrace ( );
   System.out.println ("InstantiationException");
  } catch ( IllegalAccessException e )
  {
   // TODO Auto-generated catch block   e.printStackTrace ( );
   System.out.println ("IllegalAccessException");
  } catch ( ClassNotFoundException e )
  {
   // TODO Auto-generated catch block   e.printStackTrace ( );
   System.out.println ("ClassNotFoundException");
  }
  // 连接参数与Access不同  String url = "jdbc:mysql://localhost/LearnJSP";
  // 建立连接  java.sql.Connection connection = null;
  Statement stmt = null;
  ResultSet rs = null;
  try
  {
   connection = DriverManager.getConnection ( url, "root", "011124" );
   stmt = connection.createStatement ( );
   // SQL语句   String sql = "select * from userinfo where username='" + sUserName
     + "' and userpwd = '" + sPasswd + "'";
   rs = stmt.executeQuery ( sql );// 返回查询结果  } catch ( SQLException e )
  {
   // TODO Auto-generated catch block   e.printStackTrace ( );
  }
  try
  {
   if ( rs.next ( ) )// 如果记录集非空,表明有匹配的用户名和密码,登陆成功   {
    // 登录成功后将sUserName设置为session变量的UserName    // 这样在后面就可以通过 session.getAttribute("UserName") 来获取用户名,    // 同时这样还可以作为用户登录与否的判断依据    request.getSession ( ).setAttribute ( "UserName", sUserName );
    response.sendRedirect ( "login_success.jsp" );
   } else
   {
    // 否则登录失败    //response.sendRedirect ( "MyJsp.jsp" );    response.sendRedirect ( "login_failure.jsp" );
   }
  } catch ( SQLException e )
  {
   // TODO Auto-generated catch block   e.printStackTrace ( );
  }
  try
  {
   if ( null!=rs )
   {
    rs.close ( );
   }
   if ( null!=stmt )
   {
    stmt.close ( );
   }
   if ( null!=connection )
   {
    connection.close ( );
   }
  } catch ( SQLException e )
  {
   // TODO Auto-generated catch block   e.printStackTrace ( );
  }
 }
 /**
  *
  */
 private static final long serialVersionUID = 1L;
}
 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/zieckey/archive/2007/01/21/1489323.aspx

解决方案 »

  1.   

    都在LoginServlet 里面做了,doPost方法里面啊用户名:String sUserName = request.getParameter ( "txtUserName" ); 密码:String sPasswd = request.getParameter ( "txtPassword" ); 然后拿这两个条件去数据库查询是否存在,存在则登陆ok 否则失败String sql = "select * from userinfo where username='" + sUserName 
        + "' and userpwd = '" + sPasswd + "'"; 
      rs = stmt.executeQuery ( sql );// 返回查询结果 
      

  2.   


    ……
      String sUserName = request.getParameter ( "txtUserName" ); //获取网页输入的用户名
    ……
      String sPasswd = request.getParameter ( "txtPassword" ); //获取网页输入的密码……
      connection = DriverManager.getConnection ( url, "root", "011124" ); //数据库的用户名和密码分别为root和011124
      stmt = connection.createStatement ( ); 
      // SQL语句   String sql = "select * from userinfo where username='" + sUserName 
        + "' and userpwd = '" + sPasswd + "'"; 
      rs = stmt.executeQuery ( sql );// 查询数据库中是否存在网页输入的用户数据
    ……
      

  3.   

    mysql登录进入后
    select * from userinfo ;
    可用的用户名、密码都在这里面了。
    如果报表名不存在

    MYSQL>use LearnJSP

    MYSQL>select * from userinfo ;
    如果无记录,则
    MYSQL>insert into userinfo values ('admin','admin);
    MYSQL>commit;
    然后你可以用admin,admin登录咯
      

  4.   

    数据库是:LearnJSP,和一个表:userinfo 
    表中有两个字段分别为:UserName varchar (20) not null,UserPwd varchar (20) not null 
    是不是查询用户名"txtUserName" 和密码"txtPassword"是不是存在啊???   
      

  5.   

    大哥 你真牛啊  一连帮我解决了两个问题啊 小弟真想以后多跟你请教啊 佩服啊 能加我QQ吗?以后多跟你学习学习 呵呵 我的QQ378124342