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 7621973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:20:02+00:00 2026-05-31T04:20:02+00:00

I am trying out basic login using JSP and Servlets and don’t understand how

  • 0

I am trying out basic login using JSP and Servlets and don’t understand how forwardslashes are used to indicate the path.

login.JSP is located in LoginApp/WebContent/login.jsp

LoginServlet.java is located in LoginApp/src/org/koushik/javabrains/LoginServlet.java

I have the following code in my login.jsp file –

 <form action="login" method="post">

 <br>User ID  input type="text" name="userId" />
 <br>Password <input type="password" name="password" />
 <br><input type="submit" />

 </form>

The corresponding servlet code is

  @WebServlet("/login")  // <-- forwardslash here
  public class LoginServlet extends HttpServlet 
   {
     private static final long serialVersionUID = 1L;

     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
      {
         String userId, password;

         userId = request.getParameter("userId");
         password = request.getParameter("password");

                 // more code here
      }

    }

If we see the form action, there is no forwardslash before “login”, whereas if we see the servlet annotation, there is a forwardslash before “login”. Why this difference?

  • 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-31T04:20:04+00:00Added an answer on May 31, 2026 at 4:20 am

    In

    @WebServlet("/login")
    

    The /login is a url pattern that is relative applications to the contextPath

    e.g. if your application had a context path of webapp then a request to

    http://localhost:8080/webapp/login would load the LoginServlet

    In your jsp the form action

    Is relative to the jsp page itself, and not the contextPath.

    However because your jsp is located in the webroot folder (the top level folder where your jsp’s and WEB-INF folder live)

    http://localhost:8080/webapp/login.jsp

    then the action="login" attribute in the form

    will resolve to the location

    http://localhost:8080/webapp/login

    when the form is submitted and will call the LoginServlet

    If you move the jsp into a subfolder (e.g. folder1) then action=login will not call the login servlet

    as the jsp will now be located at

    http://localhost:8080/webapp/subfolder/login.jsp
    and so action=login

    will now resolve to
    http://localhost:8080/webapp/subfolder/login

    and the servlet will not be found (remember the login servlet is relative to the context root, thats what the / means in @WebServlet("/login"))

    changing the form action to

    <form action="../login" method="post">
    

    would work.

    To avoid having to work this out in webpage forms

    most people will change the form action to look like this

    <form action="${pageContext.request.contextPath}/login" method="post">
    

    So that where ever the jsp is located the el expression

    ${pageContext.request.contextPath}/login

    will resolve to same location as the servlet defined with url pattern /login

    see What does this expression language ${pageContext.request.contextPath} exactly do in JSP EL? for more info an the el expression

    Hope this helps

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

Sidebar

Related Questions

I'm trying out this basic authentication using jQuery. But nothing happens when I enter
Know this might be rather basic, but I been trying to figure out how
Pretty basic stuff here, trying to pull the number 14.5 out of this string
I'm trying to get some basic authentication/authorization with devise/cancan with Rails. Rather than using
I'm trying to sort out an opt in mailing list system. I understand the
I've spent two days trying to make a login/logout panel in PHP. I'm using
I am trying to make a basic Hex viewer out of a TMemo, I
I am trying to do some basic scripting using ruby to log in to
I am just starting out with JSP in NetBeans 6.9.1 and im trying to
I've been trying to figure out how to get a basic HTTP Basic auth

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.