sevlet:
package servlet;
import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import bean.*;
public class Handleadmin extends HttpServlet {
private static final long serialVersionUID = 1L;
public void init(ServletConfig config) throws ServletException
{
super.init(config);
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (Exception e) { }
}
public String handleString(String s)
{
try
{
byte bb[] = s.getBytes("iso-8859-1");
s = new String(bb);
} catch (Exception ee) { }
return s;
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
Admin adminbean = null;
HttpSession session = request.getSession(true);
try
{
adminbean = (Admin) session.getAttribute("ad");
if (adminbean == null)
{
adminbean = new Admin();
session.setAttribute("ad", adminbean);
}
}
catch (Exception e)
{
adminbean = new Admin();
session.setAttribute("ad", adminbean);
}
// try
// {
String admin = handleString(request.getParameter("admin"));
String password = handleString(request.getParameter("password"));
adminbean.setAdmin(admin);
adminbean.setPassword(password);
Connection con;
PreparedStatement sql;
try
{
String uri = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=news";
String condition = "SELECT * FORM admin where admin=? and password=? ";
con=DriverManager.getConnection(uri, "sa", "sa");
if(admin.length()==0 || password.length()==0 )
{
response.sendRedirect("admin.jsp");
}
sql = con.prepareStatement(condition);
sql.setString(1, admin);
sql.setString(2, password);
ResultSet rs = sql.executeQuery();
if (rs.next())
{
if (rs.getString(1).equals(adminbean.getAdmin().toString())
&& rs.getString(2).equals(adminbean.getPassword().toString()))
{
response.sendRedirect("adminnews.jsp");
}
else
{
response.sendRedirect("admin.jsp");
}
}
// con.close();
}catch(SQLException ee){ }
// } catch (SQLException ee) { }
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doPost(request, response);
}
}
为什么这句 if(admin.length()==0 || password.length()==0 )
{
response.sendRedirect("admin.jsp");
}
放到con=DriverManager.getConnection(uri, "sa", "sa");这之后就不执行了 放到它之前就执行 还有con=DriverManager.getConnection(uri, "sa", "sa");这句之后一直到catch()之前都不执行 帮我改改
就象这段被注释掉一样
con=DriverManager.getConnection(uri,"sa", "sa");
catch(SQLException ee){ }
连接数据库是没问题的 用jsp编也没问题 可是换成sevlet 就有问题了 不知道为什么 越详细越好 回答的对的或好的 再加50分
jsp+javabean+sevlet 登陆的问题
答案:2 悬赏:30 手机版
解决时间 2021-02-10 04:44
- 提问者网友:ミ烙印ゝ
- 2021-02-09 03:47
最佳答案
- 五星知识达人网友:酒者煙囻
- 2021-02-09 04:07
看看数据库 有联 上没
全部回答
- 1楼网友:时间的尘埃
- 2021-02-09 05:40
你好!
把代码都放到一个try里 然后多重catch 你这代码不法看
希望对你有所帮助,望采纳。
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯