0


请帮忙 。下面是我将具有两个属性的请求重定向到 JSP 的代码,但以下两个属性都返回 passWord 的值。
1)user_Name

2)密码

例如,ABC 是我的用户名,XYZ 是我的密码,它来自“FORM”,但我的第二个值,即 passWord 变量的值出现在两个属性中。

在 jsp 页面内打印:

用户名:XYS(应该是 ABC 而不是 XYZ)

pass_Word: XYZ

小服务程序:

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;
 } 
}

下面是我打印 user_Name 和 pass_Word 的 JSP:

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);

提前感谢您的帮助...

4

1 回答 1

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 "",您将获得密码的值,因为用户名被覆盖。

解决方案:

你应该设置属性像

编辑小服务程序

 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

于 2013-06-10T07:40:18.253 回答