• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Redirect doPost to doGet java  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to redirect from doPost to doGet in the same Class and get session value.
below is my code who I create


   import java.io.IOException;
   import javax.servlet.ServletException;
   import javax.servlet.annotation.WebServlet;
   import javax.servlet.http.HttpServlet;
   import javax.servlet.http.HttpServletRequest;
   import javax.servlet.http.HttpServletResponse;
   import javax.servlet.http.HttpSession;
   import java.io.PrintWriter;  

   @WebServlet("/ControllerHelper")
   public class ControllerHelper extends HelperBase {
private static final long serialVersionUID = 1L;
   public ControllerHelper(HttpServletRequest request, HttpServletResponse
   response)
       throws ServletException, IOException {

       super(request, response);
   }

   protected void doPost(HttpServletRequest request, HttpServletResponse
   response)  
           throws ServletException, IOException {  
request.getSession().setAttribute("helper", this);
response.setContentType("text/html");  
     
         
       String name=request.getParameter("name");  
       String password=request.getParameter("password");  
         
       LoginBean bean=new LoginBean();  
       bean.setName(name);  
       bean.setPassword(password);  
       request.setAttribute("bean",bean);  
         
       boolean status=bean.validate();  
         
       if(status){  
        doGet(request, response); // call doGet()
       }  
       else{  
        doGet(request, response); // call doGet()
       };  
};
public void doGet(String address) throws ServletException, IOException {
       request.getSession().setAttribute("helper", this);
       PrintWriter out = response.getWriter();
       HttpSession session=request.getSession(false);
       response.setContentType("text/html");
       session.setAttribute("MyAttribute", "");
       String User=(String)session.getAttribute("MyAttribute");
       if(User=="") {
        request.setAttribute("stts_log", "Login");
        request.setAttribute("btn_modal", "exambutton");
        request.setAttribute("btn_href", "#");
       
       
       }else{
        request.setAttribute("sess", User);
        request.setAttribute("stts_log", "Logout");
        request.setAttribute("btn_modal", "");
        request.setAttribute("btn_href", "");
      }
       request.getRequestDispatcher("/view/header.jsp").include(request,
       response);
       request.getRequestDispatcher("/view/"+address).include(request,
       response);
       request.getRequestDispatcher("/view/footer.jsp").include(request,
       response);
    }
    };
 
Ranch Hand
Posts: 1327
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

public class ControllerHelper extends HelperBase {



HelperBase is your own Class, isn't it?
 
Curse your sudden but inevitable betrayal! And this tiny ad too!
ScroogeXHTML - the small and fast RTF to HTML converter library
https://coderanch.com/t/707504/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!