• 0

[JSP] How to use JavaBeans in Servlet?


Question

Hello. I am new to web programming, and recently I chose JSP for my school assignment.

Now I have already find out how to passing information between JSP and servlet, but I want to make the system more MVC (Model-View-Controller), which means JSP as "View", servlet as "Controller" and JavaBeans as "Model". I want to know how to pass information between servlet and JavaBeans? I read some tutorials, but many of them talk about using JavaBeans in JSP. Actually I want to know the syntaxes.

Could someone give me some tutorials/ suggestions? Thanks very much. :D

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0

Hi,

Here is a sample, firstly our model, lets create a Javabean called Person:

package com.antaris.demo;

public class Person implements Serializable
{
  private String name;
  public String getName() { return name; }
  public void setName(String name) { this.name = name; }

  private int age;
  public int getAge() { return age; }
  public void setAge(int age) { this.age = age; }

  public Person()
  {

  }
}

Now we know that for a POJO (Plain Old Java Object) to be considered a Javabean, it must have a public parameterless constructor, and set and get properties using getters and setters. I can't remember the requirements for serializability, I think its optional. We use that as our model. Next, we need a controller, which will instantiate that model and pass it to our view, so:

package com.antaris.demo;

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

public class MyController extends HttpServlet
{
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException
  {
	Person person = new Person();
	person.setName("Matthew");
	person.setAge(24);

	request.setAttribute("person", person);
	RequestDispatcher despatch = request.getRequestDispatcher("myTestPage.jsp");
	despatch.forward(request, response);
  }
}

The general premise here is that on a HTTP GET request, the servlet will create an instance of our Javabean (Person), set its properties, and then set it as an attribute of the HttpServletRequest instance, request. Once this has been done, we can reuse this reference, here "person" in our view, myTestPage.jsp:

<html>
  <head>
	<title>Example Java MVC using Javabeans, Servlets and JSP</title>
  </head>
  <body>
	<h1>Welcome ${person.name}, you must be ${person.age} year(s) old!</h1>
  </body>
</html>

Hope that helps, you could google for Javabeans, JSP and Servlets, or specifically Java MVC and you'd probably get some good hits.

Link to comment
Share on other sites

  • 0

Whoa, seems very easy! :o I gonna try it tomorrow!

Thanks for your tutorials!!!

P.S. Is ${person.name} a scriptlet? Should the scriptlets in myTestPage.jsp bounded by <% %>, rather than ${ }? Or it is another syntax in JSP?

Link to comment
Share on other sites

  • 0

Oh sorry, the syntax I used above is example of jsp 2.0 style expression language. It's good because it handles the type casting for you and is much cleaner than the tag based syntax.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.