• Post Reply Bookmark Topic Watch Topic
  • New Topic

Want to get result when website initially uploading rather than pressing the button for action

 
Jonas Kazlauskas
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I am learning how to program in JAVA servlet+MySQL. Decided to make a simple program where it shows who is celebrating birthday today by retrieving data from database. At the moment I managed to get it working by loading a form.html and pressing the button in it wich gets to the result.jsp and shows results there. My plan was to get a list of people who are celebrating there birthdays initially when the website is loaded/being loaded, I mean when you enter URL into your web browser and you get it instantly in browser. Is there a way to do it the way I want(JAVA+servlet+MySQL) instead of pressing a button/link?

Thank you for your time.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65516
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure, you can fetch the data in the controller for the page*. If you don't have page controllers, you should. Please read this article for information on structuring Java web applications.


* Technically, the controller should call classes in the model layer to get the data, but it all happens under the control of the controller (that's why it's called a controller).
 
Jonas Kazlauskas
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK. I read your reference. I had read some pages(150) of Head First Servlets book before, some pages(150) of MySQLs anf Java developers guide. I thank you for your time BUT I still do NOT get it. I mean yes, I understand the good things MVC brings to the table: code reuse, clearness etc. I need a specific very basic solution down here. Can you share your knowledge please?
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The servlet's doGet method can retrieve the data from the DB, put it into some appropriate data structure, set that as request attribute, and forward to a JSP that uses that data to create the HTML.

Have you used servlets to forward to JSPs before? How about request attributes? Both are covered in Head First Servlets (but possibly not in the first 150 pages).
 
Jonas Kazlauskas
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ULF, thank you. This is the right angle to solve my problem. I understand what you wrote and I am able to implement it in my code.
To answer your question - I wrote a few programs using JAVA+MySQL+servlet+jsp. But all the programs where using some input features(button, checkedlist..). And now I want to write a program that wouldnt need any input feature. User types url in browser - user sees data retrieved from DB. SO my question is - how to overcome this process of need to have a button. I tried to make this adjustment in my xml file


where servlet forwards request attribute to jsp but then I am getting an error(HTTP Status 500 - java.lang.NullPointerException) while trying to run it.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to declare your servlet, and map it to whatever URL you want to use to access it. Both these are done in web.xml
 
Jonas Kazlauskas
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand it, ULF. On the other hand since I dont have
I think that my problem lies in my web.xml. So, once again, I dont want any buttons or imputlists. Here is my Birthdays.java code:


Here is my jsp code:


And finally, my web.xml:


Console logs:
Spa 28, 2014 11:31:52 PM org.apache.catalina.core.AprLifecycleListener init
INFO: 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\Java\jre1.8.0_20\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files/Java/jre1.8.0_20/bin/client;C:/Program Files/Java/jre1.8.0_20/bin;C:/Program Files/Java/jre1.8.0_20/lib/i386;C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Vodafone\Vodafone Mobile Connect\Optimization Client\;C:\Program Files\QuickTime\QTSystem\;C:\Program Files\Java\jdk1.8.0_20\bin;;D:\eclipse;;.
Spa 28, 2014 11:31:52 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:StudentsBirthdays' did not find a matching property.
Spa 28, 2014 11:31:52 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
Spa 28, 2014 11:31:53 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Spa 28, 2014 11:31:53 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-nio-8009"]
Spa 28, 2014 11:31:53 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Spa 28, 2014 11:31:53 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1172 ms
Spa 28, 2014 11:31:53 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Spa 28, 2014 11:31:53 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/8.0.12
Spa 28, 2014 11:31:53 PM org.apache.catalina.util.SessionIdGeneratorBase createSecureRandom
INFO: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [350] milliseconds.
Spa 28, 2014 11:31:54 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
Spa 28, 2014 11:31:54 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8009"]
Spa 28, 2014 11:31:54 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 1001 ms
statement prepared
element assigned
element assigned
statement prepared
element assigned
element assigned
statement prepared
element assigned
element assigned
statement prepared and so on until terminates..

Any ideas??
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65516
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your welcome file should not be a JSP, but its controller.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!