如何在java中的servlet之间移动?

时间:2014-04-17 16:26:51

标签: java servlets

我有2个servlet,如下所示。当我运行servlet1并点击提交时,我想将参数传递给servlet 2并显示从servlet2生成的新网页,并传递参数。任何人都可以告诉我实现这一目标的最佳方法是什么。感谢。

@WebServlet("/Servlet1")
public class Servlet1 extends HttpServlet {
private static final long serialVersionUID = 1L;

    public void doGet (HttpServletRequest request, 
            HttpServletResponse response) throws IOException { 
        String name = request.getParameter("username"); //assign String variable to "username" parameter
        String address = request.getParameter("useraddress");   //assign String variable to "address" parameter
        PrintWriter out = response.getWriter();     //Creates printwriter object

        HttpSession session=request.getSession();   //create shortcut

        if ((name != null)&&(name.length() >0)&&    //if name is entered
                (String)session.getAttribute(name)!=null){//and if that name has an address associated with it already
            out.println("<h4>The username '"+name+"' is already stored for " 
                +"this session. The address of this user is '"+(String)session.getAttribute(name)+"'.</h4>"); //Print statement
            }//end if


        else if ((name!=null)&&(name.length()>0)&&(address!=null)&&(address.length()>0)){//if name and address fields are not empty
            session.setAttribute(name,address);//save the attritubes as scope variables
            out.println("<h4>The username '"+name+"' and address '"+(String)session.getAttribute(name)+"' has been registered in the system.</h4>");//Print statement
            }//end if 


        else if ((name!=null)&&(name.length()>0)&&((String)session.getAttribute(name)==null)){//if a name is entered and there is no corresponding address stored
            out.println("<h3>User not registered. Please enter a name and address.</h3>");//Print statement
            }//end if

        else if (request.getSession().isNew()==true)    //if it is not a new session
            {       //Print nothing
            }//end if

        else{       //otherwise
            out.println("<h3>Enter valid details to proceed!<h3>"); //Print this message where the other conditions are not met
            }

        out.println("</body></html>"); //close html page
        out.close();//close the PrintWriter
        }//end method
}//end class



@WebServlet("/Servlet2")
public class Servlet2 extends HttpServlet {
public void doGet (HttpServletRequest request, 
HttpServletResponse response) throws IOException, ServletException { 

    response.setContentType("text/html"); //sets content type to html
    PrintWriter out = response.getWriter();     //Creates printwriter object
    // then write the response 
    out.println("<html>" + 
    "<head><title>User Registration Details</title></head>");
    //Get the user details from the html form
    out.println("<body bgcolor=\"#ffffff\">" +
            "<h2>Please enter your name:</h2>" +"<form method=\"get\">" +   
            "<input type=\"text\" name=\"username\" size=\"25\">"+  //assign "username" as the variable for the name input field
            "<p></p>"+
            "<h2>Please enter your address:</h2>" +"<form method=\"get\">" +    //Print line
            "<input type=\"text\" name=\"useraddress\" size=\"25\">"+   //assign "useraddress" as the variable for the address input field
            "<p></p>"+
            "<input type=\"submit\" value=\"Submit\">" +    //Create submit button
            "<input type=\"reset\" value=\"Reset\">" +      //Create reset button
            "</form>");

 }
}

1 个答案:

答案 0 :(得分:0)

正如Sotirios Delimanolis在他的评论中所说,在servlet中为你的HTML做一堆out.println是错误的方法。它绝对是丑陋和维护的噩梦。所以你确实需要阅读servlets info page

但是你的提交没有做任何事情的原因是你没有给你的表单做任何动作,所以它提交回打印出表单的servlet。给它一个action属性,其中包含你希望它提交给的servlet的url:

<form method='get' action='Servlet1'>