David Garratt

Ranch Hand
+ Follow
since Aug 08, 2003
David likes ...
Mac Eclipse IDE Safari
Merit badge: grant badges
For More
Peterborough, UK
Cows and Likes
Cows
Total received
1
In last 30 days
0
Total given
0
Likes
Total received
3
Received in last 30 days
0
Total given
9
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by David Garratt

Just wanted to acknowledge that I've seen your last post and I'm working on my project,

Many thanks for the wonderful support.

Dave
1 year ago
My current project does have a servlet, and what I'm trying to grasp is within my servlet code I have an



Within my servlet code I would display a list of database connections to the user and open the jdbc link to the selected database. The jdbc connection would be per user session and would disconnect when the session times out.





My webservice

extends jakarta.ws.rs.core.Application

whereas my current servlet  

extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet, HttpSessionListener, HttpSessionAttributeListener



This is where I'm struggling to accomplish the same with the WebService code.

Dave
1 year ago

But first, are you intending to use JPA, or do you want to execute your own queries using a JDBC connection by hand?



My current solution (desktop app and servlet) both use a mechanism not unlike hibernate but of my own design which uses sql statement from a xml document and executes those. It can handle mySQL, Oracle or SQL Server and picks the appropriate sql syntax as needed.

If I could use my existing solution I would prefer to do that - but I am rather unclear as to the lifecycle of the current code, the entry point, the sequence of calls, where the execution branches when there is an exception and so forth.

I would like to know where I would put my jdbc database "connection" and "close" code - the only place I can see at the moment is within each @POST @GET method - but that seems awfully inefficient - opening and closing all the time.

I realise all these Annotations are supposed to make life easier - when you know what and when to use them all - but I'm used to knowing what's going on behind the scenes and this "abracadabra" approach to making it work with little real java code is worrying me a bit.

Dave
1 year ago
So where is the entry point for this web service - Main method equivalent - is there something I need to override in order to add my initialisation code ?

Dave
1 year ago
Would adding EclipseLink dependency to my project and reading up on JPA be heading in the right direction ?
1 year ago
Do I assume that Spring JPA is only relevant if your target is Spring Boot and won't be of interest to me and my poor little Tomcat server ?
1 year ago
My project now looks like this. I have been looking at many internet resources about Jersey and web services and how they can be made to run as a servlet. I had to admit that I'm not sure what my app is running as - if its not a servlet and if I should make changes to make it run as one. I do have experience with servlets.

Commander4jWebApplication.java


TrayController.java


TrayDto.java


web.xml


My next step will be to replace the in-memory data with a database and jdbc.  I want to find the correct place for JDBC and application initialisation code and some guidance as to making sure I don't have any concurrency issues. This app will only be used by a low number of users and it's for internal company use - not internet.

In summary,

1) Do I need to adapt this code to work as a servlet model - pro's cons - reasons for doing it - sorry I got this confused. I intend to deploy to Apache Tomcat 10
2) Where do I put my init code, jdbc, logging etc
3) Any special concurrency considerations.
1 year ago
If I want to have some initialisation code - say for jdbc in my web service application - where would that go. I see a class called ExampleWeApplication which extends jakarta.ws.rs.core.Application

Do I need to override some method to add my startup / shutdown code there ?

I am thinking about my JDBC connections etc.

Thanks

Dave
1 year ago
I have been making steady progress thanks to all the help everyone has been providing. I do have a questions regarding best practice/performance.

In one of the examples provided there was a comment about @Singleton - which I understand.

When I extend my code so that the server side is reading / writing data to a JDBC database how does one connect to the database with each web service request.

As the system is stateless am I making a connection - performing I/O - closing connection or am I using a pool of connections and making sure access to the pool is handled in a safe concurrent way.

My app is going to be used at most by 12 people at a time.

I guess the dumb frowned upon method would be to use @Singleton and put my JDBC code in there. But I am sure there is a much better way which is thread safe and I would welcome some input please.

Dave
1 year ago
I would just like to say how grateful I am and how nice it was that everyone spent so much of their time helping me with this.

Thank you

David
1 year ago
Success !! I really hope this message thread will help other people trying to learn this technology like me.

1 year ago
This is my web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://jakarta.ee/xml/ns/jakartaee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="5.0">
 <display-name>panelService</display-name>
 <welcome-file-list>
   <welcome-file>index.html</welcome-file>
   <welcome-file>index.jsp</welcome-file>
   <welcome-file>index.htm</welcome-file>
   <welcome-file>default.html</welcome-file>
   <welcome-file>default.jsp</welcome-file>
   <welcome-file>default.htm</welcome-file>
 </welcome-file-list>
</web-app>

1 year ago
Below shows the contents of the deployed folders within tomcat webapps.

Although the picture does not show it WEB-INF/classes/com/example/webapp is present and includes the 3 java classes as expected.
1 year ago
I followed your instructions. I now have my war app deployed to tomcat and can see it from within Tomcat Manager.

I didn't rename it so that I could distinguish from previous failed attempts.

panelService2-0.0.1-SNAPSHOT.war

If I open the URL via a browser (GET) I "get" the following output

http://localhost:8080/panelService2-0.0.1-SNAPSHOT/api/persons

[]



At this point I try POSTMAN - which again is new to me and I select "POST" and put in the url as the same.

http://localhost:8080/panelService2-0.0.1-SNAPSHOT/api/persons

I have not used any params or any of the other options. For reasons unknown I get 400 - Bad request.

HTTP Status 400 – Bad Request
Type Status Report

Message Bad Request

Description The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).

Apache Tomcat/10.0.7



Maybe I'm not using postman correctly.
1 year ago
I'm really sorry to take so much of everyones time on this. I've spent maybe 2-3 weeks trying to just get a simple example working. Each day I'm starting from scratch - looking at different examples on the web, trying different IDE's libraries etc and just not understanding what I've done wrong.

This is my "comfort zone" for java development. Commander4j

1 year ago