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

  • SEARCH
  • Home
  • 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 6637293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:17:44+00:00 2026-05-25T23:17:44+00:00

I understand there are a variety of ways to design web applications using java-ee,

  • 0

I understand there are a variety of ways to design web applications using java-ee, but the method that seems to make the most sense to me is model 2 or mvc where we separate the code and design into servlets and jsp pages.

Based on the research I have done, the proper method is to access a page through a servlet and then points the user in the proper direction, but i’m having a hard time wrapping my brain around this idea so please tell me if this real world example makes practical model 2 sense.

Say I have a website and the home page is a login. So here the user would go to something like http://www.mysite.com/Login (where Login is the name of the servlet). This servlet might look something like this:

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    HttpSession session = request.getSession();

    if(request.getParameter("username") != null && request.getParameter("password") != null){

        User user = new User();
        user.setUsername(request.getParameter("username"));

        try {
            user.setPassword(com.portraitcreationsnc.services.EncryptionService.getInstance().encrypt(request.getParameter("password")));
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            if(!user.userExist(user)){ //No Match.
                String errMessage = "You have entered an invalid Username/Password combination."; //Error Message
                request.setAttribute("message", errMessage);
                request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response);
                System.out.println("Login Error");
            } else {
                //Match found.
                session.setAttribute("user", user);

                System.out.println("User Login Successful");
                pageForward(request, response);
            }
        } catch (SQLException e) {
            throw new ServletException("DB error ", e);
        }

    }else{
        if(session.getAttribute("user") == null){
            request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response);
        }else{
            //User already logged in. Send to home.
            response.sendRedirect("Welcome");
        }
    }
}

private void pageForward(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
    response.sendRedirect("Welcome");
}

The main idea here is that the servlet loads initially and checks whether or not it has requestParameters. If it does not, it redirects to login.jsp which might look something like:

<%@ 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>Login</title>
</head>
<body>
<h3>Simple Login</h3>
<form action="LoginAuth" method="post">
Username <input type="text" name="username"> 
Password <input type="password" name="password">
<button type="submit">Login</button>
</form>
<%
Object errMessage = request.getAttribute("message");
if(errMessage != null){
out.println(request.getAttribute("message"));
out.println("<br>");
}
%>

<br>
If you do not have a username, <a href="register.html">click here to register.</a>
</body>
</html>

Which in turn will call the servlet again when the form is submitted and the servlet will recognize the request data, and proceed with checking the user against a database etc etc.

It seems somewhat backwards to not just load the jsp first, but in other situations it makes far more sense to load the servlet first. The research I have done concludes that with model 2 you should almost exclusively never call a jsp page, but instead direct all links to servlets who pass the user onto jsp’s. Is my design the proper way to accomplish this?

  • 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-25T23:17:45+00:00Added an answer on May 25, 2026 at 11:17 pm

    You got it right. Note that you could initially point to another servlet (let’s call it LoginDisplay), which would just dispatch to the login JSP. The JSP would submit to the LoginAuth servlet, which expects all the required parameters to be there.

    If you want to use such a model, don’t reinvent the wheel, though. There are lots of frameworks implementing this model already and providing other useful services : Stripes, Spring MVC, Struts just to name a few.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I understand that there are several ways to blend XNA and WPF within the
This question is for the java language in particular. I understand that there is
Probably not the best wording for that question but... I understand there are things
I searched a variety of sources but don't really understand the difference between using
I understand there is a method send for xmlHttpRequest objects, but I've been working
I understand that there's no universal answer to the attribute vs. element debate (and
I don't understand why there is no inheritance in Java annotations, just as Java
Why only one overload throws this exception? Little update: I understand that there was
is there any way to make IE6 understand double classes, say I have a
I understand that floating point calculations have accuracy issues and there are plenty of

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.