Rick
Rick

Reputation: 648

How to use a servlet to repsond to a request for an object

I'm sure this is easy, but I don't work with website development very often and I'm lost on this one.

I have a web application that needs to support multiple clients with different settings, icons and other content. The contents of these files are in separate directories for each client.

What I would like to do is respond to a request sent to a jsp/java servlet. The servlet will look up the proper folder location in a database (I have the database stuff working) and send the actual object to the requesting page whether it is xml, graphic or video.

How do I do that? What methods should I be using. Help I'm lost! :(

Upvotes: 1

Views: 131

Answers (3)

Kiril
Kiril

Reputation: 40345

The request and response are part of your serlvet doGet and doPost methods:

protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
    //...
}

You can use the request to store objects:

request.SetAttribute("customValue", new CustomClass());
RequestDispatcher dispatcher = request.getRequestDispatcher(/*..*/);
dispatcher.forward(request, response);

In your jsp you'll just lookup the attribute from the request:

CustomClass customValue= (CustomClass) request.getAttribute("customValue");

Updated.

Upvotes: 0

BalusC
BalusC

Reputation: 1108632

Provide an user login so that you can take action accordingly depending on the logged-in user. On login, store the found User in the session scope by HttpSession#setAttribute(). Then, on every request check the logged-in user by HttpSession#getAttribute(). E.g.

User user = (User) session.getAttribute("user");
List<Movie> movies = movieDAO.findMoviesByUser(user);
request.setAttribute("movies", movies);
request.getRequestDispatcher("/WEB-INF/movies.jsp").forward(request, response);

Upvotes: 1

duffymo
duffymo

Reputation: 308743

Write a servlet that does the following in the doPost and/or doGet method:

  1. Gets parameters out of the HTTP request indicating what they want.
  2. Interact with the database and model objects to get the requested data.
  3. Add the data to JSP scope or write HTML to the response output stream

You'll have to package the servlet into a WAR file. Write a web.xml to declare your servlet and map it to request URLs.

That's it.

Upvotes: 0

Related Questions