• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JSP/Servlet how to keep the URL constant?

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good day! I solve tasks here That one -> Task #199 Employees Web App

I used JSP/Servlet and OpenShift as a HOST. My solution
The problem is when I add new name the URL is changed from http://caewa-sergeylotvin.rhcloud.com/index.jsp to http://caewa-sergeylotvin.rhcloud.com/dbSelSrv.my?name=NewNameExample. index.jsp transform to dbSelSrv.my where dbSelServ.my is my Servlet.
How can I avoid this changes? Because for checker I need it work like that:

If your page is http://caewa-sergeylotvin.rhcloud.com/index.jsp

Calling http://caewa-sergeylotvin.rhcloud.com/index.jsp should display the page

Calling http://caewa-sergeylotvin.rhcloud.com/index.jsp?kill should clear all names except the company founder

Calling http://caewa-sergeylotvin.rhcloud.com/index.jsp?name=New+Name should add a new name



Can someone help?
p.s.: I need to solve it with JAVA, not AJAX or php or other.
 
Saloon Keeper
Posts: 15490
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, you're using a GET to post data. Use POST as your form method.

You also shouldn't send commands as request parameters. Make separate actions for separate commands. index.jsp shouldn't be a catch-all.

That means to delete all names, you could POST to an URL like http://caewa-sergeylotvin.rhcloud.com/clear-all.jsp, and to add a new name you could POST to http://caewa-sergeylotvin.rhcloud.com/add.jsp
 
Sergey Lotvin
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stephan van Hulst, I agree. It is OK for real Web App. But that task will be checked automatically by some program. And that program must see:

http://caewa-sergeylotvin.rhcloud.com/index.jsp
when first request the page, than exactly
http://caewa-sergeylotvin.rhcloud.com/index.jsp?name=WhateverNewName
when Add new Employee.

And exactly
http://caewa-sergeylotvin.rhcloud.com/index.jsp?kill
When delete.
 
Stephan van Hulst
Saloon Keeper
Posts: 15490
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't have those exact URLs without client-side scripting.

You must change the action of your form element to action="index.jsp", but if you need to send data using query parameters (which is a BAD idea), then you can't prevent the form from also adding other inputs (like the submit button) to the query parameter string.
 
Stephan van Hulst
Saloon Keeper
Posts: 15490
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was mistaken. You can do this if you remove the name attribute from the submit button. That means your form needs to look like this:

It's still a bad idea though...
 
Sergey Lotvin
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I have to refuse to use Servlet?

If I do like this:



My Servlet will not be started, though I got the URL desired As Servlet was not started I have no new name in DB.
 
Stephan van Hulst
Saloon Keeper
Posts: 15490
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then your web server is configured wrongly. The index.jsp URL should be directed to your servlet. If the servlet determines it's appropriate, it can then forward the request to the actual JSP.
 
Sergey Lotvin
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I can't understand. if I change to <form action="index.jsp" method="get"> then action will call index.jsp and not dbSelSrv.my
Then I have to change url-pattern from dbSelSrv.my to index.jsp?
 
Stephan van Hulst
Saloon Keeper
Posts: 15490
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes.

Your actual index.jsp file should also be under your WEB-INF, because it shouldn't be a publicly accessible resource.
 
Stephan van Hulst
Saloon Keeper
Posts: 15490
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What framework are you using? If you're using something like Spring MVC, you can just direct all requests to a dispatch servlet, which will then forward the requests to controller classes using annotations. This is much less messy than what you've got now.
 
Sergey Lotvin
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do not use any frameworks. JSP/Servlet only. Can you help me with the code for index.jsp insertion in web.xml?
 
Stephan van Hulst
Saloon Keeper
Posts: 15490
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's assume your web.config looks like this:
You can then forward a request to a JSP like this:
 
Stephan van Hulst
Saloon Keeper
Posts: 15490
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Really though, you should be using a proper MVC framework to dispatch requests for you.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this an academic exercise where you are limited to doing this in this way, or real production code?
 
Sergey Lotvin
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, you've convinced me to use additional framework. But for me clear understanding - if I use servlet my URL in any case will change. And if I have to stay with the same URL the only way to use client scripts (JS, Spring MVC). Am I right?
 
Sergey Lotvin
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Is this an academic exercise where you are limited to doing this in this way, or real production code?


It's not an academic exercise . But I'm afraid I could be limited by the HOST (but looks like openshift allow to use Spriong) - at first, and by my knowledge as a second. I'm newbie in JAVA.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then why are you trying to keep the URL constant in the first place? It's not a reasonable nor customary thing to do. It sounds like the sort of silly thing an academic exercise would ask someone to do so they "can figure out how to do it", but it's not something one would see in a real-world erg app. So what's the actual goal here?
 
Sergey Lotvin
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Then why are you trying to keep the URL constant in the first place? It's not a reasonable nor customary thing to do. It sounds like the sort of silly thing an academic exercise would ask someone to do so they "can figure out how to do it", but it's not something one would see in a real-world erg app. So what's the actual goal here?



Because the checker works. checker is some kind of program, that start with http://blablabla/index.jsp then add names using GET http://blablabla/index.jsp?name=NewName . But my Web App works with servlet and index.jsp changes to MyСorrespondingServlet.lol and URL from
http://caewa-sergeylotvin.rhcloud.com/index.jsp
become
http://caewa-sergeylotvin.rhcloud.com/MyСorrespondingServlet.lol?name=NewName
but checker can't know about MyСorrespondingServlet.lol
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What exactly is "checker" and why is it important that you conform to it?
 
Sergey Lotvin
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I solve tasks on codeabbey.com That one -> Task #199 Employees Web App
(see first message with links)
And I did the task. It works. But checker can't get it. Checker is software that do some algorithm to check if the task was done correctly.
 
Stephan van Hulst
Saloon Keeper
Posts: 15490
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's what the flow should be with a real web application:

  • Client sends request to server.
  • Server's dispatcher analyzes the request, creates an instance of the controller you have associated with that request, and forwards the request to the controller.
  • The controller interacts with the business or persistence layer of your application to make changes (in case of POST) and retrieve data.
  • If the request is a GET, the controller forwards the data to the view (in this case, a JSP).
  • If the request is a POST, the controller redirects the browser so it makes a GET to a different URL where it can view the updated state of the application. All of these steps happen again.

  • In your case, you only have a GET request to show the employees, with the special case that it can change the state of the application if the request includes the parameters kill or name. NEVER do this in real life.

    The code you need is simple enough that you probably don't have to include an entire MVC framework, but in a real world application I would.

    What you need to do now is edit my code example so that the servlet retrieves a list of employees, sets it as an attribute in the request, and forwards the request to the JSP. All the JSP should do is display the list of employees. It shouldn't interact with the database in any way.

    Don't worry about killing or adding employees yet, just show us that you can forward data from a servlet to a JSP.
     
    Sergey Lotvin
    Ranch Hand
    Posts: 46
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Stephan van Hulst, yes, and I did it this way from the only beginning.

    WEB.XML


    index.jsp



    Servlet dbSelSrv



    class to select:

     
    Sergey Lotvin
    Ranch Hand
    Posts: 46
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    BTW
    It

    doesn't work. It produces an infinite loop. When it is deploying index.jsp call servlet with name index.jsp who forward on index.jsp etc. if I create a clone of start index.jsp and put it in /WEB-INF/jsp/index.jsp and then

    it soesn't work also.
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 15490
    363
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Sergey,

    ItDoesntWorkIsUseless. Please tell us exactly what's happening. Here's what I have, and it works just fine:

    web/WEB-INF/web.config:
    src/java/com/example/EmployeeServlet.java:
    web/WEB-INF/jsp/index.jsp:
    I then browse to http://localhost:8080/example/index.jsp (/example is my application's context path), and I get a lovely "Hello World!" message.

    I very much prefer to use /employees as the URL pattern for my servlet though, so I can browse to http://localhost:8080/example/employees?name=John%20Doe.
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 15490
    363
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    When you've gotten this to work, build a List<String> representing the names of employees in your servlet class, and then pass that as an attribute to your JSP.

    DO NOT access the database from your JSP.
     
    Sergey Lotvin
    Ranch Hand
    Posts: 46
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Cool! now it works as I wished
    task solved
    Many many thanks, Stephan van Hulst!
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 15490
    363
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Well done! Can you show us your servlet and your JSP?
     
    Sergey Lotvin
    Ranch Hand
    Posts: 46
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    web.xml




    index.jsp



    servlet



     
    Stephan van Hulst
    Saloon Keeper
    Posts: 15490
    363
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You are still accessing your database from your JSP. Don't do this. JSPs should only be used to display data passed to them.
     
    Sergey Lotvin
    Ranch Hand
    Posts: 46
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Yep, totally agree. Moreover, to use DB here is overweight. Later I will try List, as you advised. Generally I used DB also to learn this part: connect to DB, select/insert/delete from JAVA code.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic