Step 1: Download MySQL
The first step is to download MySQL and get it up and running. Follow the installation instructions, start up the server, and make sure you can log in using "mysql -u root -p" in the terminal. This link provides a good resource for a refresher on SQL.
Step 2: Download Connector/J
Connector/J can be downloaded here. All we really need is mysql-connector-java-5.1.20-bin.jar in the src/lib folder. This jar file is the JDBC driver that lets us communicate with MySQL using Java code.
Step 3: Establish a connection to MySQL using JDBC
Create a new Java project and add mysql-connector-java-5.1.20-bin.jar to the build path. This link provides a good tutorial on JDBC.
Step 4: Download Tomcat
Tomcat 7 can be downloaded here. Unpack and follow the instructions in RUNNING.txt. Once Tomcat is running, you should be able to see the default web page at http://localhost:8080. Add mysql-connector-java-5.1.20-bin.jar to apache-tomcat-7/lib.
Step 5: Understand the directory structure of a Java web application
Java web applications should have the following directory structure
Directory structure of a Java web app |
The root folder is the name of your Java project. Under the root folder, there should be another folder called WEB-INF. Under the WEB-INF folder, there should be a classes folder, a lib folder, and a web.xml file. Java source files and compiled .class files belong in the classes folder. Libraries belong in the lib folder. The web.xml file is used by Tomcat to map a URL to a servlet.
Step 6: Write a simple Java application using JDBC and servlets
Create a new Java project using the directory structure above. If you don't know where to begin with JDBC and servlets, make sure Tomcat is running and go to http://localhost:8080/examples/servlets. Look at the code samples and execute them. Don't forget to add apache-tomcat-7/lib/servlet-api.jar to your project build path.
I created a to-do list web application. It queries a JDBC/MySQL backed database for a list of items, displays it in a table, and lets you add or delete items. The full source code is available at my Github repository here.
Screenshot of my web app |
Configure web.xml to map the URL to your servlet class. Below is my sample web.xml file.
When a client visits http://localhost:8080/simple-servlet/example, the request is handled by the application server. Tomcat scans for the servlet-mapping containing the "/example" URL pattern, looks up the servlet-name which is "todo", matches the servlet-name to the right servlet , and routes the HTTP request to TodoServlet.class. Then TodoServlet.class executes its logic and returns a response back to the client through Tomcat.
Step 8: Package web application into WAR file and deploy on Tomcat
First, compile all source files. In a terminal/console, navigate to your project's root directory. Then type in "jar cvf simple-servlet.war .". This creates a WAR (Web application ARchive) which is simply a jar file with a .war extension. Drop this war file into your apache-tomcat-7/webapps directory and test by going to http://localhost:8080/simple-servlet/example.
That's it! Your database-backed web application is up and ready to go.
Note: Creating HTML in servlets is actually not a good practice. Ideally, you should use some type of view technology like JavaServer Pages or JavaServer Faces to move data between the view and logic layers. Also, the database login username and password should be passed in to the program as parameters instead of being hardcoded. For the purpose of keeping it quick and simple, I did not follow these guidelines in my project.
First, compile all source files. In a terminal/console, navigate to your project's root directory. Then type in "jar cvf simple-servlet.war .". This creates a WAR (Web application ARchive) which is simply a jar file with a .war extension. Drop this war file into your apache-tomcat-7/webapps directory and test by going to http://localhost:8080/simple-servlet/example.
That's it! Your database-backed web application is up and ready to go.
Note: Creating HTML in servlets is actually not a good practice. Ideally, you should use some type of view technology like JavaServer Pages or JavaServer Faces to move data between the view and logic layers. Also, the database login username and password should be passed in to the program as parameters instead of being hardcoded. For the purpose of keeping it quick and simple, I did not follow these guidelines in my project.
A developer, it is beneficial to understand what goes on beneath these layers of abstraction.web design tutorials
ReplyDeleteA well-formed blog that connects with its audience in all the possible ways.
ReplyDeleteresponsive wordpress website
Creating simple web application can be difficult
ReplyDelete- Amna
Software Development Company in Toronto
There is so much in this article that I would never have thought of on my own. Your content gives readers things to think about in an interesting way.
ReplyDeletemason soiza
When creating a relational database, you can not only extend the data, you can define the domain of possible values in a data column and any additional constraints that you want to apply to the value of your data. This makes your information significantly more valuable and user friendly.DB Designer
ReplyDeleteGreat Article
ReplyDeleteJava Project Ideas for Final Year Students
FInal Year Project Centers in Chennai
JavaScript Training in Chennai
Java Training in Chennai
TreasureBox is operated by a group of young, passionate, and ambitious people that are working diligently towards the same goal - make your every dollar count, as we believe you deserve something better.
ReplyDeletebedroom furniture nz
pregnancy pillow nz
chicken coops nz
Thank you for taking the time to publish this information very useful! freelance web designer london
ReplyDeleteWe have sell some products of different custom boxes.it is very useful and very low price please visits this site thanks and please share this post with your friends. web design Farmington Hills MI
ReplyDeleteReally I enjoy your site with effective and useful information. It is included very nice post with a lot of our resources.thanks for share. i enjoy this post. Homepage erstellen lassen
ReplyDeleteHello, I have browsed most of your posts. This post is probably where I got the most useful information for my research. Thanks for posting, maybe we can see more on this. Are you aware of any other websites on this subject. Swiss web agency
ReplyDeleteSCCM online training
ReplyDeleteSCOM online training
MSBI online training
salesforce online training
Mulesoft online training
Webmethods online training
Thanks, this is generally helpful.
ReplyDeleteStill, I followed step-by-step your method in this
dot net training
dot net online training hyderabad
That is really nice to hear. thank you for the update and good luck. website redesign
ReplyDeletenice post.scom training
ReplyDeletescom online training
Thanks for the blog article.Thanks Again. Keep writing.
ReplyDeletejava course
learn java online