package sime.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
public 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", "woshipis" );
stmt = connection.createStatement ( );
// SQL语句 String sql = "select * from logintest 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;
}驱动我看了教程把驱动文件的JAR包,放在WEB-INF下,结果还是不能运行,我想就是驱动的问题,可是还是不知道错在哪儿,求解释
解决方案 »
- 关于hibernate
- 实现自动补全功能
- Mule 3.1.1 运行提示DeploymentService: Application directory check interval: 5000请问什
- ssh框架中, 请问怎么实现它的上传和下载文件 谢谢
- 各位大侠帮帮忙...JSP问题..急~~~~~~~~~~~
- 实现由程序自动下载服务器端指定路径名称的文件和自动上传客户端指定路径和名称的文件的方法,急啊!!!
- 菜鸟求助java读取html中table
- lomboz安装的问题,谢谢。。。。在线等
- ibatis批量导入报错
- 路径不出错的情况下一直是servlet 404错误 该怎么办?
- EJB3.0 中文问题
- 关于相对路径
Class.forName ( "com.mysql.jdbc.Driver" );要不直接就换个包吧
按1楼方法试试,看你jar包有原始版本org.gjt.mm.mysql.Driver的目录结构么
还有楼主的mysql密码有深意啊
{
}