• 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

HTTP Status 404 - /Login/login.html

 
Ranch Hand
Posts: 151
1
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Below is the code for my second servlet which is not running and gives an error message as "HTTP Status 404 - /Login/login.html, description The requested resource is not available".



Following is the web.xml file:-



Please help. The other servlet works fine but not this one. Thanks!!

 
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
Have you worked through the list for this issue in the ServletsFAQ? If not, that's your next step.

Hint: if your servlet is not in a package, that's likely the cause.
 
Abhimanyu Jain
Ranch Hand
Posts: 151
1
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have checked the FAQs but it did not work out for me.

I don't know how to compile a package that's why I did not put it in a package. I tried but could not compile it. My first servlet was not in a package as well but it works fine, I could not understand what's wrong with this one.
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, in your web.xml you have mapped the servlet to the url login.do
So I would expect a 404 for login.html (unless you have an html file there as well...)

Does /Login/login.do work ?
 
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

Abhimanyu Jain wrote:I have checked the FAQs but it did not work out for me.

I don't know how to compile a package that's why I did not put it in a package.


Well, then you didn't follow the FAQ. If you don't know how to compile Java, you're not ready for servlets yet. You need to learn the basics first.
 
Abhimanyu Jain
Ranch Hand
Posts: 151
1
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
/login.do is for servlet mapping. I checked login.do does not work either.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I repeat: you must follow all the guidelines in the FAQ. If you don't know how to use packages, you must learn this first.
 
Abhimanyu Jain
Ranch Hand
Posts: 151
1
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I learnt how to compile a package and followed the ServletsFAQ again and it worked.



It is working now.
 
Abhimanyu Jain
Ranch Hand
Posts: 151
1
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now the servlet runs but does not show the output as "welcome, username. Enjoy browsing the site".

After submitting the username and password the page remains blank. Please help!!!
 
Abhimanyu Jain
Ranch Hand
Posts: 151
1
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is throwing an exception as "Unable to process the request. Please try after some time".

 
Abhimanyu Jain
Ranch Hand
Posts: 151
1
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Below is the main reason why it is not working.

HTTP Status 500 - Initialization failed, unable to get database connection

type Exception report

message Initialization failed, unable to get database connection

description The server encountered an internal error that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Initialization failed, unable to get database connection
demoServlet.LoginServlet.init(LoginServlet.java:27)
javax.servlet.GenericServlet.init(GenericServlet.java:158)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:537)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1085)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658)
org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1556)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1513)
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Unknown Source)
note The full stack trace of the root cause is available in the Apache Tomcat/8.0.15 logs.

I am using MySQL, what could be the reason for this error? How can I fix it? Help!! Help!!
 
Abhimanyu Jain
Ranch Hand
Posts: 151
1
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I fixed the 500 error. The mysql-connector file was missing from the lib folder. But still getting the exception "Unable to process the request. Please try after some time". Help!! Help!!
 
Abhimanyu Jain
Ranch Hand
Posts: 151
1
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Following code fixed the issue. I put the "connection.close()" in the finally block and it started working.

 
Ranch Hand
Posts: 672
4
Eclipse IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could still be getting an exception.

By the way,

Abhimanyu Jain wrote:It is throwing an exception as "Unable to process the request. Please try after some time".


This is not a good approach, you are masking the exception and not seeing what exception you are hitting at. use e.printStackTrace() method and post us the tomcat logs so we can find find out what's the root cause of the exception.
 
Rancher
Posts: 989
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't put database connection logic in a servlet. Create a separate class that contains the database logic and test that it works without involving the servlet.
Once you are happy with that then use that class from the servlet.
Also building that html from the servlet may be ok for first tests but you will want to use a JSP (or other view file) for designing your html in.
 
Abhimanyu Jain
Ranch Hand
Posts: 151
1
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Prasad Krishnegowda: I checked without finally block as well it is working just fine.

@E Armitage: Thank you!! I will make sure of that the DB connection is in a different class.
 
Abhimanyu Jain
Ranch Hand
Posts: 151
1
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Facing the same issue again "Unable to process the request"

Following is the Tomcat log file:-

2015-01-21 13:17:11 Commons Daemon procrun stderr initialized
21-Jan-2015 13:17:12.465 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version: Apache Tomcat/8.0.15
21-Jan-2015 13:17:12.465 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built: Nov 2 2014 19:25:20 UTC
21-Jan-2015 13:17:12.465 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server number: 8.0.15.0
21-Jan-2015 13:17:12.465 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name: Windows 7
21-Jan-2015 13:17:12.465 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version: 6.1
21-Jan-2015 13:17:12.465 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture: amd64
21-Jan-2015 13:17:12.465 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JAVA_HOME: C:\Program Files\Java\jre1.8.0_25
21-Jan-2015 13:17:12.465 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version: 1.8.0_25-b18
21-Jan-2015 13:17:12.465 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor: Oracle Corporation
21-Jan-2015 13:17:12.465 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE: C:\Program Files\Apache Software Foundation\Tomcat 8.0
21-Jan-2015 13:17:12.465 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME: C:\Program Files\Apache Software Foundation\Tomcat 8.0
21-Jan-2015 13:17:12.465 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=C:\Program Files\Apache Software Foundation\Tomcat 8.0
21-Jan-2015 13:17:12.465 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=C:\Program Files\Apache Software Foundation\Tomcat 8.0
21-Jan-2015 13:17:12.465 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.endorsed.dirs=C:\Program Files\Apache Software Foundation\Tomcat 8.0\endorsed
21-Jan-2015 13:17:12.465 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=C:\Program Files\Apache Software Foundation\Tomcat 8.0\temp
21-Jan-2015 13:17:12.465 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
21-Jan-2015 13:17:12.465 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=C:\Program Files\Apache Software Foundation\Tomcat 8.0\conf\logging.properties
21-Jan-2015 13:17:12.465 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: exit
21-Jan-2015 13:17:12.465 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Xms128m
21-Jan-2015 13:17:12.465 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Xmx256m
21-Jan-2015 13:17:12.465 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Apache Software Foundation\Tomcat 8.0\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Program Files\Common Files\Microsoft Shared\Windows Live;C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Live;.;C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\Program Files (x86)\Intel\OpenCL SDK\3.0\bin\x86;C:\Program Files (x86)\Intel\OpenCL SDK\3.0\bin\x64;C:\HibLib\mysql-connector-java-5.1.34-bin.jar;C:\Program Files\Java\jdk1.8.0_25\bin;C:\Program Files\Java\jre1.8.0_25;C:\Program Files (x86)\Windows Live\Shared;;.
21-Jan-2015 13:17:12.636 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-9090"]
21-Jan-2015 13:17:12.699 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
21-Jan-2015 13:17:12.714 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["ajp-nio-8009"]
21-Jan-2015 13:17:12.714 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
21-Jan-2015 13:17:12.714 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 728 ms
21-Jan-2015 13:17:12.746 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service Catalina
21-Jan-2015 13:17:12.746 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.0.15
21-Jan-2015 13:17:12.746 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory C:\Program Files\Apache Software Foundation\Tomcat 8.0\webapps\docs
21-Jan-2015 13:17:13.136 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory C:\Program Files\Apache Software Foundation\Tomcat 8.0\webapps\docs has finished in 390 ms
21-Jan-2015 13:17:13.136 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory C:\Program Files\Apache Software Foundation\Tomcat 8.0\webapps\examples
21-Jan-2015 13:17:13.744 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory C:\Program Files\Apache Software Foundation\Tomcat 8.0\webapps\examples has finished in 608 ms
21-Jan-2015 13:17:13.744 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory C:\Program Files\Apache Software Foundation\Tomcat 8.0\webapps\First
21-Jan-2015 13:17:13.775 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory C:\Program Files\Apache Software Foundation\Tomcat 8.0\webapps\First has finished in 31 ms
21-Jan-2015 13:17:13.775 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory C:\Program Files\Apache Software Foundation\Tomcat 8.0\webapps\host-manager
21-Jan-2015 13:17:13.822 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory C:\Program Files\Apache Software Foundation\Tomcat 8.0\webapps\host-manager has finished in 47 ms
21-Jan-2015 13:17:13.822 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory C:\Program Files\Apache Software Foundation\Tomcat 8.0\webapps\Login
21-Jan-2015 13:17:13.994 INFO [localhost-startStop-1] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
21-Jan-2015 13:17:13.994 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory C:\Program Files\Apache Software Foundation\Tomcat 8.0\webapps\Login has finished in 172 ms
21-Jan-2015 13:17:13.994 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory C:\Program Files\Apache Software Foundation\Tomcat 8.0\webapps\manager
21-Jan-2015 13:17:14.025 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory C:\Program Files\Apache Software Foundation\Tomcat 8.0\webapps\manager has finished in 31 ms
21-Jan-2015 13:17:14.025 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory C:\Program Files\Apache Software Foundation\Tomcat 8.0\webapps\ROOT
21-Jan-2015 13:17:14.040 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory C:\Program Files\Apache Software Foundation\Tomcat 8.0\webapps\ROOT has finished in 15 ms
21-Jan-2015 13:17:14.040 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-9090"]
21-Jan-2015 13:17:14.056 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-nio-8009"]
21-Jan-2015 13:17:14.072 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 1356 ms
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after statement closed.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:377)
at com.mysql.jdbc.Util.getInstance(Util.java:360)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:935)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:924)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:870)
at com.mysql.jdbc.StatementImpl.checkClosed(StatementImpl.java:445)
at com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:3951)
at demoServlet.LoginServlet.doPost(LoginServlet.java:50)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:644)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:537)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1085)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658)
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1556)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1513)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)

Please help!!!
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have connection and prepared statement as instance variables in your servlet and are initializing them in the servlet init. Then when a request is made the connection is used and closed. When the next request comes the connection has now been closed.
You should never have instance variables in Servlets. Servlets are supposed to be stateless. You should also never open connections and keep them for future requests like that. When you need a connection, get one, use it and close it. Let the problem of managing connections be handled by your container's pooling settings. So you really should move all that database logic from the servlet into a dao class and not try to keep instance connections in your servlet.
 
Abhimanyu Jain
Ranch Hand
Posts: 151
1
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand that. But I need to fix this code somehow. Please help. I will be grateful!!

 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Think about what I wrote above and you will see the problem. Do you know when the init method of a servlet is run? Do you understand that one servlet can serve multiple requests?
 
Abhimanyu Jain
Ranch Hand
Posts: 151
1
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, single instance of a servlet can service multiple request for that particular servlet. And the init() method is called by the container once the instance of the servlet is created.

I think I am closing the connection to DB that's causing the issue. Am I correct?
 
E Armitage
Rancher
Posts: 989
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abhimanyu Jain wrote:Yes, single instance of a servlet can service multiple request for that particular servlet. And the init() method is called by the container once the instance of the servlet is created.

I think I am closing the connection to DB that's causing the issue. Am I correct?



Yes, and if you remove the close then you will never close that connection! As I said, connections should not be kept open for a long time. When you need a connection get one, use it and close it. Remove the instance variables that you have in the servlet.
 
Abhimanyu Jain
Ranch Hand
Posts: 151
1
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you!! One last thing, is it good practice to code the database connection in the configuration file and get it using "getServletContext().getInitParameter()" method? Will it be fine instead of creating a DBConnection class?
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abhimanyu Jain wrote:Thank you!! One last thing, is it good practice to code the database connection in the configuration file and get it using "getServletContext().getInitParameter()" method? Will it be fine instead of creating a DBConnection class?



Servlets are deployed on a servlet container. Servlet containers have documentation on how to configure datasources that can used to get connections in applications. So typically the connection settings are defined in the servlet container's configuration file rather than in your application's web.xml
 
Abhimanyu Jain
Ranch Hand
Posts: 151
1
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where do we find the servlet container's configuration file?
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abhimanyu Jain wrote:Where do we find the servlet container's configuration file?



It depends on what your container is. Suppose your container is called ABC (typical examples are Tomcat, Jetty, Jboss, Glassfish). Google for "How to setup datasource on ABC" and you should find the instructions.

 
Abhimanyu Jain
Ranch Hand
Posts: 151
1
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you!! I will google it. Thanks a lot.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic