日期:2014-05-17 浏览次数:20687 次
<%@ page contentType="text/html; charset=gb2312"%> <% request.setCharacterEncoding("gb2312"); %> <!-- 该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=gb2312"> <meta http-equiv="Content-Language" content="ch-cn"> </head> <body bgcolor=cyan> <!-- Form 用来提取用户填入并提交的信息--> <form method="POST" name="frmLogin" action="LoginServlet"> <h1 align="center">用户登录</h1><br> <div align="center">用户名: <input type="text" name="txtUserName" value="" size="20" maxlength="20" onfocus="if(this.value=='Your name')this.value=";"><br>密码: <input type="password" name="txtPassword" value="" size="20" maxlength="20" onfocus="if(this.value=='Your password')this.value=";"><br> <input type="submit" name="Submit" value="提交" onClick="validateLogin();" > <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>
/** * 该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.HttpSer