请帮忙 。下面是我的代码,用两个属性将请求重定向到JSP,但以下两个属性都返回passWord的值。
1)USER_NAME
2)Pass_word
例如,ABC是我的用户名,XYZ是我的密码,它来自“FORM”但我的第二个值,即passWord变量的值出现在两个属性中。
在jsp页面内打印:
user_Name:XYS(应该是ABC而不是XYZ)
pass_Word:XYZ
servlet:
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
public class JndiConn extends HttpServlet implements Servlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
String userName=request.getParameter("Username");
String passWord=request.getParameter("password");
String user_Name="";
request.setAttribute(user_Name,userName);
String pass_Word="";
request.setAttribute(pass_Word,passWord);
RequestDispatcher rd=request.getRequestDispatcher ("GetParameter.jsp");
System.out.println(userName);
System.out.println(passWord);
response.setContentType("text/html");
PrintWriter writer = response.getWriter();
Connection connection = getConnection();
if (connection != null) {
String sql = "SELECT count(1) FROM scott.login_user where login_id ='"+userName+"' and password='"+passWord+"'";
String sql1 = "select SYSDATE from dual";
System.out.println(sql);
try{
PreparedStatement statement = connection.prepareStatement(sql);
ResultSet rs = statement.executeQuery();
while (rs.next()) {
int number=rs.getInt("count(1)");
System.out.println("Result value is "+number);
if(number==1)
{
System.out.println("THE LOGIN SUCCESS FOR::"+userName);
String sql2 = "INSERT INTO SCOTT.LOGIN_USER VALUES(3,'VINOTH','vinoth55','SCOTT',to_date('08/06/13','DD/MM/RR'),105)";
PreparedStatement statement1 = connection.prepareStatement(sql2);
ResultSet rs1 = statement1.executeQuery();
rd.forward(request, response);
}
else{
System.out.println("THE LOGIN FAILURE FOR "+userName);
rd.forward(request, response);
}
}
connection.close();
}catch(Exception e){
System.out.println(e);
}
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
private Connection getConnection() {
Connection connection = null;
try {
InitialContext context = new InitialContext();
DataSource dataSource = (DataSource) context.lookup("vinothprd");
connection = dataSource.getConnection();
} catch (NamingException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
}
下面是我的JSP打印user_Name和pass_Word:
JSP:
String user_Name="";
String userName=(String)request.getAttribute(user_Name);
String pass_Word="";
String passWord=(String)request.getAttribute(pass_Word);
System.out.println("user_Name: "+userName);
System.out.println("pass_Word: "+passWord);
提前感谢您的帮助......
答案 0 :(得分:0)
在您设置的Servlet
中,使用空值设置属性。
String user_Name="";
request.setAttribute(user_Name,userName);
String pass_Word="";
request.setAttribute(pass_Word,passWord);
首先,这是错误的,我不知道你为什么要这样做。
在JSP中,当您访问request.getAttribute(user_Name); // which is empty ""
时,
当用户名被覆盖时,您将获得密码的值。
解决方案:
您应该设置类似
的属性编辑Servlet
String userName=request.getParameter("Username");
String passWord=request.getParameter("password");
String user_Name="userName";
request.setAttribute(user_Name, userName);
String pass_Word="passWord";
request.setAttribute(pass_Word, passWord);
然后在JSP
String user_Name="userName"
String userName=(String) request.getAttribute(user_Name);
String pass_Word="passWord";
String passWord=(String)request.getAttribute(pass_Word);
请参阅:http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#setAttribute