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.


EmpInfo.html

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

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

</html>

It will look like this:


EmpInfo.html


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.


insert.jsp


<%@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);

Class.forName("com.mysql.jdbc.Driver");
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+"')");
if(i>0)
{
%>

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

<%
}
else
{
%>
 <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.)

2 comments :

 

© 2014 DieHardTechy . All rights resevered. Designed by Diehardtechy