你好我已经创建了一个servlet来处理数据库操作但是每当servlet加载它什么都不做时,这里只出现一个白色屏幕是我的servlet文件代码`保护无效
doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Connection successfull");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String op=request.getParameter("operation");
if(op=="create")
{
try {
String usrnm=request.getParameter("usr");
String pswd=request.getParameter("pass");
String tp=request.getParameter("type");
Connection con=DriverManager.getConnection(url, host, password);
Statement stmt=con.createStatement();
String sql="insert into login (username,password,type) values('"+usrnm+"','"+pswd+"','"+tp+"')";
stmt.executeUpdate(sql);
RequestDispatcher ct=request.getRequestDispatcher("createuser.jsp");
ct.forward(request, response);
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else if(op=="update")
{
try {
String usrnm=request.getParameter("usr");
String pswd=request.getParameter("pass");
String tp=request.getParameter("type");
Connection con=DriverManager.getConnection(url, host, password);
Statement stmt=con.createStatement();
String sql="update login set password='"+pswd+"' where username='"+usrnm+"'";
stmt.executeUpdate(sql);
RequestDispatcher rd=request.getRequestDispatcher("createuser.jsp");
rd.forward(request, response);
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else if(op=="login"){
try {
String usrnm=request.getParameter("user");
String pswd=request.getParameter("pass");
String type=null;
Connection con=DriverManager.getConnection(url, host, password);
Statement stmt=con.createStatement();
String sql="select * from login where username='"+usrnm+"' AND password='"+pswd+"'";
ResultSet rs=stmt.executeQuery(sql);
if(rs.first())
{
type=rs.getString(4);
switch(type){
case "admin":
RequestDispatcher admin=request.getRequestDispatcher("admin.jsp");
admin.forward(request, response);
break;
case "selector":
RequestDispatcher selector=request.getRequestDispatcher("selector.jsp");
selector.forward(request, response);
break;
case "player":
RequestDispatcher player=request.getRequestDispatcher("player.jsp");
player.forward(request, response);
break;
case "comentator":
RequestDispatcher comentator=request.getRequestDispatcher("comentator.jsp");
comentator.forward(request, response);
break;
}
}
else{
RequestDispatcher rd=request.getRequestDispatcher("dummylogin.jsp");
rd.forward(request, response);
} } catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}`
我在这里创建了两个jsp文件是login.jsp的代码
'<div class="content_item">
<h1 align="left">Welcome user, login here</h1>
<br/><br/><br/>
<form action="dbcon" method="post">
<table cellspacing="15" cellpadding="15">
<tr><td>
<font size="5px" color="silver"><b>Username: </b></font></td><td><input type="text" name="user" id="user" style="height: 18px; width: 200px;" ></td></tr>
<tr><td> <font size="5px" color="silver" style="font: bolder;"><b>Password: </b></font></td><td><input type="password" name="pass" id="pass" style="height: 18px; width: 200px;" ></td></tr>
<tr><td></td><td></td></tr>
<tr><td><input type="submit" onclick="return validate()" value="Login" style="height: 31px; width: 85px;"></td>
<td><input type="reset" value="Reset" style="height: 31px;width: 85px;"></td></tr>
</table></form>
<input type="hidden" value="login" name="operation">
</div> '
答案 0 :(得分:0)
在您的代码中,不会执行if
更改
if(op=="login")
到
if(op.equals("login"))
,因为
==
会比较参考
equals()
会比较值