我贴上代码吧。还有就是我没连数据库前是能跳转页面的,写上连接数据库的代码就不行了logcl是业务逻辑层package com.login;import javax.servlet.http.*; import java.sql.*; import java.io.*; public class LoginCl extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) {
Connection cc = null; Statement sm = null; ResultSet rs = null; try { String u = req.getParameter("username"); String p = req.getParameter("passwd");
Class.forName("com.mysql.jdbc.Driver"); cc = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306;databaseName=sqdb", "root", "1"); sm = cc.createStatement(); rs = sm.executeQuery("select top 1 * from users where username='"+u+"' and passwd='"+p+"'");
package com.login;import javax.servlet.http.*; import java.sql.*; import javax.servlet.* ; import java.io.*; public class LoginCl extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
Connection cc = null; Statement sm = null; ResultSet rs = null;
try { String u = req.getParameter("username"); String p = req.getParameter("passwd");
Class.forName("com.mysql.jdbc.Driver"); cc = DriverManager.getConnection("jdbc:mysql://localhost:3306/=sqdb", "root", "1"); sm = cc.createStatement(); rs = sm.executeQuery("select * from users where username='"+u+"' and passwd='"+p+"'"); System.out.println("执行查询,获取校验结果...") ; if (rs.next()) {
1、你看看后台报错了没有 ?2、还有就是你查看一些你的业务逻辑是否有问题 ?3、在你的业务逻辑代码中多加一些调试信息 ,查看后台 ,看看程序运行到哪里 ,容易找出问题所在 ;
import java.sql.*;
import java.io.*;
public class LoginCl extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) {
Connection cc = null;
Statement sm = null;
ResultSet rs = null; try {
String u = req.getParameter("username");
String p = req.getParameter("passwd");
Class.forName("com.mysql.jdbc.Driver");
cc = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306;databaseName=sqdb", "root", "1");
sm = cc.createStatement();
rs = sm.executeQuery("select top 1 * from users where username='"+u+"' and passwd='"+p+"'");
if (rs.next()) {
HttpSession hs = req.getSession(true);
hs.setMaxInactiveInterval(20);
hs.setAttribute("name", u);
res.sendRedirect("wel?un="+u+"&pw="+p);
}
else {
res.sendRedirect("login");
}
}
catch (Exception ex) {
ex.printStackTrace();
}
finally {
try {
if (rs !=null) {
rs.close();
}
if (sm !=null) {
sm.close();
}
if (cc != null) {
cc.close();
}
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
public void doPost(HttpServletRequest req, HttpServletResponse res) {
this.doGet(req, res);
}
}
把这个换成:
jdbc:mysql://127.0.0.1:3306/sqdb
import java.sql.*;
import javax.servlet.* ;
import java.io.*;
public class LoginCl extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
Connection cc = null;
Statement sm = null;
ResultSet rs = null;
try {
String u = req.getParameter("username");
String p = req.getParameter("passwd");
Class.forName("com.mysql.jdbc.Driver");
cc = DriverManager.getConnection("jdbc:mysql://localhost:3306/=sqdb", "root", "1");
sm = cc.createStatement();
rs = sm.executeQuery("select * from users where username='"+u+"' and passwd='"+p+"'");
System.out.println("执行查询,获取校验结果...") ;
if (rs.next()) {
HttpSession hs = req.getSession(true);
//hs.setMaxInactiveInterval(20);
hs.setAttribute("name", u);
System.out.println("执行重定向到wel...") ;
res.sendRedirect("wel?un="+u+"&pw="+p);
}else {
System.out.println("执行重定向到login...") ;
res.sendRedirect("login");
}
}catch (Exception ex) {
ex.printStackTrace();
}finally {
try {
if (rs !=null) {
rs.close();
}
if (sm !=null) {
sm.close();
}
if (cc != null && !cc.isClosed()) {
cc.close();
}
}catch (Exception ex) {
ex.printStackTrace();
}
}
}
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
this.doGet(req, res);
}
}
<Resource name="jdbc/mysql" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000" username="root" password="root"
driverClassName="com.sql.jdbc.Driver" url="jdbc:mysql://locolhost:3306/lhdb" />
放到context标签里,里面意思你能看懂,然后在自己的配置文件里面把这个连接源配上就ok了
<Resource name="jdbc/mysql" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000" username="root" password="root"
driverClassName="com.sql.jdbc.Driver" url="jdbc:mysql://locolhost:3306/lhdb" />建议还是用startup启动tomcat,这样有助于看到控制台信息,不少错误可以排查
<description> DB Connection </description>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>不好意思 浏览器不给力 没复制上 web.xml里配置的内容,如上