Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JSP page - fill bean - to servlet

JSP page - fill bean - to servlet

Scheduled Pinned Locked Moved Web Development
javadatabasecomhelpquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BASONJS
    wrote on last edited by
    #1

    Hi all, I have been trying to find this out for 4 days straight, doing all types of research and yet I cannot find it, although it seems like one of the most fundamental things I can think of. I am new to jsps and servlets but not new to java, so please be patient with me _____________________________________________________ SCENARIO: 1) I have an index.jsp page with a form with 1 field for the user to fill up. 2) I have a bean which I am trying to fill up from the index page itself. 3) I have a servlet that handles the request _____________________________________________________ INDEX PAGE:

    <%@ page import="com.java2s.*"%>

    What's your name?

    _____________________________________________________ BEAN:

    package com.java2s;

    public class bean {
    String username;

    public void setUsername( String value )
    {
        username = value;
    }
    
    public String getUsername() 
    { 
        return username; 
    }
    

    }

    _____________________________________________________ SERVLET:

    package com.java2s;

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

    public class MyServlet extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException 
    {
    
        HttpSession session = request.getSession(true);
        // Get the bean from the session
        bean myBean = (bean)session.getAttribute("bean");
    
        // if the bean is not in session instantiate a new bean
        if (myBean == null)
        {
            myBean = new bean();
            myBean.setUsername("FAILED");
        }
    
        request.setAttribute("bean", myBean);
        request.getRequestDispatcher("test.jsp").forward(request, response);
    }
    

    }

    ____________________________________________________ PROBLEM: I get the bean in the servlet, that is to say: bean myBean = (bean)session.getAttribute("bean"); actually returns an object and not null, thats not a problem. The proble

    H 1 Reply Last reply
    0
    • B BASONJS

      Hi all, I have been trying to find this out for 4 days straight, doing all types of research and yet I cannot find it, although it seems like one of the most fundamental things I can think of. I am new to jsps and servlets but not new to java, so please be patient with me _____________________________________________________ SCENARIO: 1) I have an index.jsp page with a form with 1 field for the user to fill up. 2) I have a bean which I am trying to fill up from the index page itself. 3) I have a servlet that handles the request _____________________________________________________ INDEX PAGE:

      <%@ page import="com.java2s.*"%>

      What's your name?

      _____________________________________________________ BEAN:

      package com.java2s;

      public class bean {
      String username;

      public void setUsername( String value )
      {
          username = value;
      }
      
      public String getUsername() 
      { 
          return username; 
      }
      

      }

      _____________________________________________________ SERVLET:

      package com.java2s;

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

      public class MyServlet extends HttpServlet {

      protected void processRequest(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException 
      {
      
          HttpSession session = request.getSession(true);
          // Get the bean from the session
          bean myBean = (bean)session.getAttribute("bean");
      
          // if the bean is not in session instantiate a new bean
          if (myBean == null)
          {
              myBean = new bean();
              myBean.setUsername("FAILED");
          }
      
          request.setAttribute("bean", myBean);
          request.getRequestDispatcher("test.jsp").forward(request, response);
      }
      

      }

      ____________________________________________________ PROBLEM: I get the bean in the servlet, that is to say: bean myBean = (bean)session.getAttribute("bean"); actually returns an object and not null, thats not a problem. The proble

      H Offline
      H Offline
      hansoctantan
      wrote on last edited by
      #2

      Your code is different in my code... I cant figure out whats the error in that but If you want to fill a bean heres some simple codes... JSP

      <%@ page language="java" contentType="text/html; charset=UTF-8"
      pageEncoding="UTF-8"%>
      <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
      <html>
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>Insert title here</title>
      </head>
      <body>
      <form action="myServlet" method="post">
      <input type="text" name="name"/>
      <input type="submit"/>
      </form>
      </body>
      </html>

      Servlet

      package samplePack;

      import java.io.IOException;
      import javax.servlet.ServletException;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;

      /**
      * Servlet implementation class myServlet
      */
      public class myServlet extends HttpServlet {
      private static final long serialVersionUID = 1L;

      /\*\*
       \* @see HttpServlet#HttpServlet()
       \*/
      public myServlet() {
          super();
          // TODO Auto-generated constructor stub
      }
      
      /\*\*
       \* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
       \*/
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      	// TODO Auto-generated method stub
      }
      
      /\*\*
       \* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
       \*/
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      	// TODO Auto-generated method stub
      	myBean bean = new myBean(request.getParameter("name"));
      	System.out.println(bean.getName());
      }
      

      }

      BEAN

      package samplePack;

      public class myBean {
      private String name = null;

      public myBean(String name) {
      	this.name = name;
      }
      
      public String getName() {
      	return name;
      }
      
      public void setName(String name) {
      	this.name = name;
      }
      

      }

      hope this help...

      modified on Monday, April 5, 2010 10:54 PM

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups