简体   繁体   中英

How to make my html work in the servlet code to redirect to another page?

 protected void doGet(HttpServletRequest request, HttpServletResponse response) 
                     throws ServletException, IOException {     
    PrintWriter out = response.getWriter();

    HttpSession session = request.getSession(); //create a session
    session.setAttribute("sessionname", "sessionvalue");

    out.println("Session value = " +session.getAttribute("sessionname"));
    out.println("Session ID is " +session.getId());
    out.println(session.isNew()); //checks whether the session is new or old
    out.println(session.getMaxInactiveInterval()); //Default 30 minutes 1800secs
    //session.invalidate();

    Cookie ck= new Cookie("Cookiename", "cookievalue");
    response.addCookie(ck);

    out.println("<html>");
    out.println("<body>");
    out.println("<form action=SecondServlet>");
    out.println("<input type=submit />");
    out.println("</form>"); 
    out.println("</body>");
    out.println("</html>");
}

SecondServlet.java

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    response.getWriter().append("Served at: ").append(request.getContextPath());
    PrintWriter out =response.getWriter();

    Cookie[] cookies=request.getCookies();

    for(Cookie ck: cookies){
        out.println(ck.getName());
        out.println(ck.getValue());
    }
}

I'm trying to create a submit button which redirects to another servlet class. But the submit is not being created how do i change it. These both servlet classes are working fine individually.

you may do it like add action on submit of button

public void HttpServletResponse.sendRedirect(String location)

throws IOException

Can you try following code:

    response.sendRedirect("home.html"); // Redirects to home

Or

    request.getRequestDispatcher("/WEB-INF/page.jsp").forward(request, response);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM