求助,这个程序有问题吗,不能正常跳转
package func;
import java.io.IOException;
import java.sql.*;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import func.User;
public class Signin extends HttpServlet {
private static final long serialVersionUID = 1L;
private String CONTENT_TYPE = "text/html; charset=GB2312";
Connection con=null;
public Signin()
{
String sDBdriver="sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr="jdbc:odbc:db";
try{
Class.forName(sDBdriver);
con=DriverManager.getConnection(sConnStr);
}
catch(Exception e){
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType(CONTENT_TYPE);
request.setCharacterEncoding("gb2312");
String s1=request.getParameter("num");
String s2=request.getParameter("password");
try {
Statement stm = con.createStatement();
ResultSet result = stm.executeQuery("select * from register where num='"+s1+"' and password='"+s2+"'");
if(result.next()){
RequestDispatcher dispatcher=request.getRequestDispatcher("/success.jsp");
dispatcher.forward(request, response);
}
else{
RequestDispatcher dispatcher=request.getRequestDispatcher("/signin.jsp");
dispatcher.forward(request, response);
}
con.close();
} catch (Exception e) {}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
doGet(request,response);
}
}
[解决办法]
RequestDispatcher dispatcher=request.getRequestDispatcher("/success.jsp");
dispatcher.forward(request, response)
把两句合并起来试试
RequestDispatcher dispatcher=request.getRequestDispatcher("/success.jsp").forward(request,response)还不行把错误信息列出来
[解决办法]
System.out.println("异常:"+e.getMessage());
}
[解决办法]
异常,,,,错误信息 是?
[解决办法]
是不是最后连接关闭抛异常了?
[解决办法]
package func;
import java.io.IOException;
import java.sql.*;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import func.User;
public class Signin extends HttpServlet {
private static final long serialVersionUID = 1L;
private String CONTENT_TYPE = "text/html; charset=GB2312";
Connection con=null;
public Signin()
{
String sDBdriver="sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr="jdbc:odbc:db";
try{
Class.forName(sDBdriver);
con=DriverManager.getConnection(sConnStr);
}
catch(Exception e){
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String sDBdriver="sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr="jdbc:odbc:db";
try{
Class.forName(sDBdriver);
con=DriverManager.getConnection(sConnStr);
}
catch(Exception e){
}
response.setContentType(CONTENT_TYPE);
request.setCharacterEncoding("gb2312");
String s1=request.getParameter("num");
String s2=request.getParameter("password");
try {
Statement stm = con.createStatement();
ResultSet result = stm.executeQuery("select * from register where num='"+s1+"' and password='"+s2+"'");
if(result.next()){
RequestDispatcher dispatcher=request.getRequestDispatcher("/success.jsp");
dispatcher.forward(request, response);
}
else{
RequestDispatcher dispatcher=request.getRequestDispatcher("/signin.jsp");
dispatcher.forward(request, response);
}
con.close();
} catch (Exception e) {}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
doGet(request,response);
}
}
//大概是con对象为空的原因,把构造函数东西考下来
[解决办法]
异常一定要处理的,
调试一定要加断点的,
这俩个都做好了,一般的异常很简单的
[解决办法]
获取当前路径看看 跳转的路径是否有问题,楼主代码没有错误的话,应该出现在跳转的路径上,可以尝试打印打印路径,仅供参考
[解决办法]
select * from register where num='"+s1+"' and password='"+s2+"'"
1、你这句sql能在sql server里执行么?
把是s1 和s2 给自己匹配个参数查询下
2、在这包下func.Signin.doGet(Signin.java:47)这句话的47行是哪句话?
ps: 个人认为是你 这个数据库 表register 里 字段 num 是int 类型的 而你num传递进去的参数是String类型 需要强制转换 【但是我不知道你password是什么类型的 具体请查看数据库的设计表结构 姑且认为是varchar的】 这样转换
int num1 = Integer.parseInt(s1);
把num1和s2传递进去查询
变成这样
select * from register where num='"+num1+"' and password='"+s2+"'"
3、如果还不行我做一个改动你试试
从jsp获取s1 s2 那不变,
下面从Statement stm = con.createStatement(); 开始改动
java.sql.PreparedStatement ps = conn.prepareStatement("select * from register where num = ? and password = ? "); ps.setString(1,s1); //如果ps.setString(); 不行换成 int num1 = Integer.parseInt(s1);// ps.setInt(1,num1); 【看你num字段是int还是varchar的】ps.setString(2,s2); java.sql.ResultSet rs = ps.executeQuery();//然后下面都相同了 变量名你自己换下。
[解决办法]
我又查了查资料
当往数据库中添加相同主键的记录时会报这个异常。
又看了看你的代码
也可能有没有关闭连接的原因引起。
你的con 关闭了 其他的没有关闭 stm result
if(stm != null){
stm.close(); 【try catch一定要捕获 并且处理异常】
}
if(result != null){
result.close();【try catch一定要捕获 并且处理异常】
}
if(con != null){
con.close();【try catch一定要捕获 并且处理异常】
}
你代码try catch 记得要捕获 而且要处理异常
如果不捕获你永远都不知道自己处于错误里而迷茫没报异常。