Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5949827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:18:22+00:00 2026-05-22T17:18:22+00:00

I’m trying to learn how to build web applications using Java EE 6, but

  • 0

I’m trying to learn how to build web applications using Java EE 6, but I’m struggling to understand the best way to pass information between the components of a typical MVC2 design.

The way I understand it, the MVC2 pattern using Java EE would be: the data is the model, the controller would be a servlet, and the view would be a JSP. This is just one example of course.

So I’ve written the following three pieces and I know how to install them in the server I’m using (Tomcat 7), and the entry point would be the html file below. I’m struggling with how the servlet forwards it’s response to the JSP, and how that JSP gets sent back to the client browser.

The HTML file (demo.html):

<html> 
  <head>
    <title>MVC2 Demo</title>
  </head>
  <body>
    <form method='post' action='/mvc2-demo/DemoServlet'>
      <h1>   MVC2 Demo   </h1>

      Name: <input type='text' name='input_name' size='50'>
      <br><br>
      Age:  <input type='text' name='input_age' size='10'>
      <br><br>

      <input type='submit' value='SUBMIT INFO'>
    </form>
  </body>
</html>

The servlet (DemoServlet.java):

import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DemoServlet extends HttpServlet {

    public void doPost(HttpServletRequest request, HttpServletResponse response) {

        response.setContentType("text/html");

        try {

            PrintWriter pw = response.getWriter();

            String name = request.getParameter("input_name");
            String age = request.getParameter("input_age");

            pw.println("Information received by Servlet: " + name + " : " + age);

            // forward this response to Demo.jsp...

            pw.close();

        } catch (Exception e) {

            System.out.println("Cannot get writer: " + e);
        }
    }
}

The JSP (Demo.jsp):

<html>

  <head><title> JSP Demo </title></head>

  <body>

    <h1>JSP Demo</h1>

      <ul>

         <%= get response forwarded from servlet.. %>

      </ul>
  </body>

</html>

The entry point is an HTML page which displays a simple form with two input fields (one for someone’s name, the other for their age) and a submit button. When the user hits Submit, the form sends it data to the DemoServlet. The servlet then pulls the data from the HTTP request payload and saves it in some local String variables. The part I commented out is where I’d like to somehow forward this information to the JSP. And once I do that, does the JSP automatically get sent to the client? What triggers that?

Thanks for your help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T17:18:22+00:00Added an answer on May 22, 2026 at 5:18 pm

    In MVC, a servlet should only control the request/response. It should not print anything to the response. That’s the responsibility of the view. So the following lines in the servlet violates the MVC principle:

    response.setContentType("text/html");
    PrintWriter pw = response.getWriter();
    pw.println("Information received by Servlet: " + name + " : " + age);
    

    Get rid of them all.

    You need to store the information which you’d like to display in the JSP in the request scope and then forward the request/response to the JSP. Since the parameters are already available to JSP by ${param} you don’t need to store anything in this particular case. Just forwarding to the JSP is enough. For demonstration purposes, I will add another attribute to the request:

    request.setAttribute("message", "Information successfully processed");
    request.getRequestDispatcher("/WEB-INF/Demo.jsp").forward(request, response);
    

    The attribute value is available by the attribute name in EL (Expression Language) as ${message}.

    Then the JSP, it is basically part of the HTTP response. When the servlet method finishes and a forward() has taken place, the servletcontainer will execute the JSP code. JSP offers a template to write HTML/CSS/JS the way as you want it to be sent to the client (webbrowser). The use of Scriptlets <% %> is however discouraged since it allows you to write code in such way that it violates the MVC principle. You should be using EL only ${}.

    So, replace

    <%= get response forwarded from servlet.. %>
    

    by

    <p>Those parameters are submitted: ${param.input_name} : ${param.input_age}</p>
    <p>Information received by Servlet: ${message}</p>
    

    See also:

    • Our Servlet wiki page – contains a Hello World according MVC
    • How to avoid Java code in JSP files – contains extensive background information
    • Design patterns in Java web applications – advanced topic on MVC
    • JSF versus JSP/Servlets – think before homebrewing, Java EE 6 offers JSF as real MVC framework
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Seemingly simple, but I cannot find anything relevant on the web. What is the
We're building an app, our first using Rails 3, and we're having to build
I have thousands of HTML files to process using Groovy/Java and I need to
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.