0

I'm beginner in Servlets/JSP and now trying to do a little web-project but faced some issues which I can't solve. I'd like to ask you to give an idea of correct structure of the project (see the image). enter image description here

The main part of its project is couple of tables with editable data which is sent through servlet to DB. Rows can be added and deleted from table as same as frоm DB. I have the list of contractors also - by clicking on each I have relevant tables with data. I consider the following structure: each row in the table (Orders/Invoices) is a form which is submited and sent to the servlet when the data is entered and saved in DB. It is clear for me. I don't know how to make request to a servlet when I click on any contractor from the list to get in response the relevant table. (I tried to make it by XMLHttpRequest but it doesn't work properly). More over, If I understand it correct, the response should be in JSP which create page with relevant table for shosen contractor. Is it possible to get JSP response by using Ajax?) Browsing stackoverflow to find needed answers I got that the best way is to use jQuery (both for editable table and communication with server) but as I said I'm newcomer and would like to pay the most attention to java/servlet/jsp/jdbc in the first place and only then come to front-end technologies. I would appreciate a lot for ideas regarding the structure of the project.

Aravin
  • 4,126
  • 1
  • 23
  • 39
aime
  • 117
  • 1
  • 3
  • 12
  • what Application server an what version are you going to use ? – Leo Mar 05 '14 at 16:16
  • 1
    In all fairness, the question is too broad... frankly. – Tiny Mar 05 '14 at 16:24
  • I use Tomact as web-server. Regarding application server I haven't decided yet. I agree that it is to broad but I can't understand what should I learn and do without having the layout of whole system – aime Mar 05 '14 at 16:45
  • You would like to review this Q/A: [How to use Servlets and Ajax?](http://stackoverflow.com/q/4112686/1065197) – Luiggi Mendoza Mar 05 '14 at 16:55

2 Answers2

0

Now probably nobody create applications in raw Servlets/JSP. Tables, lists, buttons and other components you have out of the box in JSF. IMHO best library for that is Primefacs. If you not like JSF, try GWT, Vaadin. If i can give you advice try this project schema JSF(presentation layer) ->EJB/Spring(service layer) -> Hibernate to persist data. Of course you may do this in JSP and XMLHttpRequest but for what? It's not effective, better and faster approach, is writing software in higher abstraction level.

Damian S.
  • 128
  • 1
  • 7
  • 1
    I agree that is the better way, but as I know servlet/jsp/jdbc technologies is the base for Sring MVC/Hibernate – aime Mar 05 '14 at 16:47
  • Thank you, guys! I decided to dig into jsp to understand what this technology can do. – aime Mar 06 '14 at 12:43
-1

Is it possible to get JSP response by using Ajax?

Yes, the response of the AJAX call will be the content of the DIV that you want to update. In your case, when you click contractor # 3 for example, the table will be updated. So your AJAX call will hit a servlet which will forward to a JSP. JSP will return the table content.

<div id="orders">

JSP return content here.... orders table thing for the chosen contractor. 

</div>

JSP will have content like.

<table>
<tr><td>${"param1"}</td><td>${"param2"}</td></tr>
</table>

Do not use XMLHttpRequest directly. If you are allowed to do so, use jQuery. Disclaimer: I have not tried this. Please check exact syntax etc. I am trying to help you get started.

RuntimeException
  • 1,593
  • 2
  • 22
  • 31
  • Thank you for help! Is that correct that then JSP returns table content it is possible update only the table, not the whole static page, right? – aime Mar 05 '14 at 16:54
  • *Is it possible to get JSP response by using Ajax?* **No**, it is not. You can receive a JSON response, a XML response, even plain text response, but not a JSP response. A JSP response is part of a forwarding which cannot be mixed with ajax. – Luiggi Mendoza Mar 05 '14 at 16:54
  • aime, yes. right. see the link below and see the first answer. it is jquery though, but the essence is the same. it means - on success, update the DIV with responseText. The responseText is returned by the JSP (or php in that example) @LuiggiMendoza, what I understood is that whether JSP can return an HTML response that can be used to update the div. http://stackoverflow.com/questions/10362235/update-div-with-the-result-of-an-ajax-call – RuntimeException Mar 05 '14 at 17:17
  • You may return HTML but that's not JSP. By your answer, you can think you could return a String that holds EL inside it (those `${}` things) but this won't be parsed by client side since it isn't processed by the server. – Luiggi Mendoza Mar 05 '14 at 17:22
  • @LuiggiMendoza, This is not what I meant. The jsp will contain those ${} things, but these will be evaluated by the server before the response is printed to the out stream ! I clearly state "content of JSP" i.e. content the .jsp file will have. – RuntimeException Mar 05 '14 at 17:31
  • @SatishMotwani if you do that with ajax, it won't work. Test it. – Luiggi Mendoza Mar 05 '14 at 17:31
  • @LuiggiMendoza Maybe the ${} thingy wont work (never used EL with Ajax, there may be something I am unaware of). What I know works for sure is that out.println statements in Servlet will work with AJAX, I have done it myself. In case of JSP, the container will process the JSP, generate Html content and ultimately write to the out stream. I am not sure why it will not do it in case of AJAX. If you know, please let me know Why it does not work. Also, will replacing the EL with request.getParameter or getAttribute in JSP work. IIRC, it works. Not sure about new versions of jsp or tomcat. – RuntimeException Mar 05 '14 at 17:51
  • @SatishMotwani `PrintWriter out = response.getWriter();` works because that's what you have to do in server side: write the response. But this doesn't mean you're returning JSP code at all (which is what I'm telling you since my first comment). So, in short: *Is it possible to get JSP response by using Ajax?*, answer is **NO** since JSP **must** be parsed by the server (parse scriptlets, EL, JSP tags, and on...), but the ajax response won't be processed in this way by the server, the text you put on it will be returned to the client. Again, test it. – Luiggi Mendoza Mar 05 '14 at 19:30
  • @SatishMotwani what you can return from an ajax response is data in **text** or **binary** format, then treat this data in your client e.g. in a browser using javascript. – Luiggi Mendoza Mar 05 '14 at 19:34
  • @LuiggiMendoza I guess we are interpreting the question (or English language) differently :-). OP says - [[ More over, If I understand it correct, the response should be in JSP which create page with relevant table for shosen contractor. Is it possible to get JSP response by using Ajax? ]] - I understand that OP wants to generate a text/html response using JSP and that response will be updated in the DIV. You take it as - the response text itself is JSP code. – RuntimeException Mar 06 '14 at 05:22