Saturday, June 21, 2014

How to configure Tomcat and run first JSP application. (Without any IDE)

4:40 PM

Before you proceed with your first program of JSP, please make sure you have following software's installed.

1.Jdk 1.6+
2.MySQL 5.0+
3.Tomcat 7.0+

Steps to install Tomcat:

1. Download tomcat server from apache's official website.

2. Install the server.

3. Provide port number of your choice or be it default, make a not of whatever the port number you are using.

4. Go to Tomcat 7.0 folder of your installation directory,lib,and add servletapi.jar file in to class path.

5. If you are using MySQL as your database ,make sure to download mysql-connector-java-5.1.30.jar and place it in Tomcat 7.0\lib\ folder.

6. Start the server:

You can start the server in any of the following way.

A. Go to Tomcat 7.0\bin\ directory and right click on Tomcat7 & run as administrator.

this will give you the console view of Tomcat. (Feature to Debug easily. )
Console View of tomcat

B. Search in windows , right click on configure tomcat\ run as administrator.Click start. 

7.Server should start successfully.

8. To check if server is properly installed or not.

A. Open your browser
B. go to http://localhost:portnumber (port number is same which you provided in step 3.)
C. if you have properly installed , you will see the home page of Tomcat.

Write a simple JSP application to add id, first_name, last_name into database.

What you need to do. 

1.Create a folder say (FirstApp) in Tomcat 7\webapps\ directory.

2. Copy WEB-INF folder from Tomcat 7.0\webapps\examples folder and paste it in FirstApp folder which you created above.

3. Create a table say employee with e_id, first_name, last_name column in MySQL database.

4. Create a HTML file (say EmpInfo.html)and place it in Tomcat 7.0\webapps\FirstApp folder.


<form action="insert.jsp">
<table cellspacing="10px" border="2">

Enter Emp. ID:
<input type="text" name="t1" />
Enter first name:
<input type="text" name="t" />
Enter last name:
<input type="text" name="t3"/>
<td colspan=2 align="right">
<input type="submit" name="b1" value="Submit" />


It will look like this:


5. Write a JSP program (insert.jsp) to insert record in database which user has entered in EmpInfo.html and place it in Tomcat 7.0\webapps\FirstApp folder.


<%@page language="java" contentType="text/html" import="java.sql.*" %>
String eid=request.getParameter("t1");
String fname=request.getParameter("t2");
String lname=request.getParameter("t3");
int empid=Integer.parseInt(eid);

Connection con=DriverManager.getConnection("jdbc:mysql://localhost/ttest","root","root");
Statement st=con.createStatement();
int i=st.executeUpdate("INSERT INTO employee (eid,first_name, last_name) values ("+empid+",'"+fname+"','"+lname+"')");

 <h1>"Value added successfully"  </h1>

 <h1>"Value not added successfully"  </h1>


How to execute. 

1.Start tomcat server.
2.Launch browser.
3.type http://localhost:portnumber/FirstApp/EmpInfo.html
4.Enter some value and click enter.
5.A page with message as Value added successfully will be displayed. 

If you run into any problem, feel free to ask. (If possible please attach screen shot of problem.)



© 2014 DieHardTechy . All rights resevered. Designed by Diehardtechy