0% found this document useful (0 votes)
3 views28 pages

Java J2EE Guide for Thinking Machines

This document serves as a reference for attendees of Java J2EE classroom sessions at Thinking Machines, detailing the setup and configuration of Tomcat servers and the development of web applications. It includes step-by-step instructions for creating a simple web application, handling session tracking, and implementing form validation using Java servlets. The document emphasizes the importance of attending theory sessions for understanding the examples provided.

Uploaded by

shsbhajak
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views28 pages

Java J2EE Guide for Thinking Machines

This document serves as a reference for attendees of Java J2EE classroom sessions at Thinking Machines, detailing the setup and configuration of Tomcat servers and the development of web applications. It includes step-by-step instructions for creating a simple web application, handling session tracking, and implementing form validation using Java servlets. The document emphasizes the importance of attending theory sessions for understanding the examples provided.

Uploaded by

shsbhajak
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Thinking Machines – Java – J2EE Page 1

This documentation is
for reference purpose
only and is for those
who have attended the
classroom sessions at
Thinking Machines.
• During your classroom session appropriate theory needs to be written against
each example.

• You are required to bring this book daily for your classroom sessions.

• If you try to understand the examples without attending theory sessions then
may god help you.
Thinking Machines – Java – J2EE Page 2

[Link]. Topic Page


1 Our first webapplication [Link] (Tomcat 4)
2 Session tracking – Hidden form field
3 Session tracking – URL rewriting
4 Session tracking – cookies & Switching to Tomcat 7 or Tomcat 8
5 Session tracking – Session Object
Thinking Machines – Java – J2EE Page 3

Configuring Tomcat 4
Download [Link], unzip it, then unzip tomcat4, copy the tomcat4 folder (the one which contains
the bin folder) to c:\

Now I am assuming that the following path exists

c:\tomcat4\bin

Move to c:\tomcat4\bin folder. Edit [Link] and see to it that the first two lines are are as follows.

set CATALINA_HOME=c:\tomcat4
set JAVA_HOME=c:\jdk1.8

Note : I am assuming that you have jdk 1.8 installed and the c:\jdk1.8\bin path exists.

Now create a shortcut to c:\tomcat4\bin\[Link] named as Tomcat4.

Double click the shortcut to start the server, you should see the following

Note : If firewall prompts something, select (allow) option.


Now start browser and in the address bar type

[Link] and you should see the following.


Thinking Machines – Java – J2EE Page 4

[Link]
Now close the server window and the browser window.
Now let us create our first website [Link]

move to c:\tomcat4\webapps and create the following structure

[Link]\WEB-INF\classes

Note : WEB-INF is upper case

Now in c:\tomcat4\webapps\[Link] folder create the following files.

[Link] (location c:\tomcat4\webapps\[Link])


<!Doctype html>
<html lang='en'>
<head>
<meta charset='utf-8'>
<title>[Link]</title>
</head>
<body>
<center>
<h1><i>Welcome to a cool site</i></h1>
<h3>Ujjain</h3>
<h6><u>Madhya Pradesh</u></h6>
<b>India</b>
<br>
<br/>
<a href='/[Link]/[Link]'>Begin</a>
</center>
</body>
</html>
[Link] (location c:\tomcat4\webapps\[Link])
<!Doctype html>
<html lang='en'>
<head>
<meta charset='utf-8'>
<title>[Link]</title>
<script>
function ramu(f)
{
var nn=[Link];
if([Link]==0)
{
alert('Name required'); [Link]();
return false;
}
Thinking Machines – Java – J2EE Page 5

var v='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz .';


[Link]("nameErrorSection").innerHTML="";
var e=0;
while(e<[Link])
{
if([Link]([Link](e))==-1)
{
[Link]("nameErrorSection").innerHTML="Invalid data in name";
[Link]();
return false;
}
e++;
}
if([Link][0].checked==false && [Link][1].checked==false)
{
alert("Select gender");
return false;
}
if([Link]==0)
{
alert("Select a city");
[Link]();
return false;
}
return true;
}
</script>
</head>
<body>
<center>
<h2>Personal Information</h2>
<form action='/[Link]/servlet/aaa' onsubmit='return ramu(this)'>
<table border='1'>
<tr><td>
Name</td><td>
<input type='text' name='nm' id='nm' >
<span id='nameErrorSection'></span>
</td></tr><tr><td>
Gender</td><td>
Male <input type='radio' name='sex' id='ml' value='M'>
&nbsp;&nbsp;&nbsp;
Female <input type='radio' name='sex' id='fe' value='F'>
</td></tr><tr><td>
Select city</td><td>
<select name='ct' id='ct'>
<option value='-1'>&lt;Select&gt;</option>
<option value='101'>Pune</option>
Thinking Machines – Java – J2EE Page 6

<option value='102'>Ujjain</option>
<option value='103'>Indore</option>
</select></td></tr></tr><tr>
<td colspan='2' align='center'>
<input type='submit' value='Save'></td></tr></table>
</form>
</center>
</body>
</html>
[Link] (location c:\tomcat4\webapps\[Link]\WEB-INF\classes)
import [Link].*;
import [Link].*;
import [Link].*;
public class aaa extends HttpServlet
{
public void doGet(HttpServletRequest rq,HttpServletResponse rs)
{
try
{
String nn=[Link]("nm");
String ss=[Link]("sex");
String cc=[Link]("ct");
[Link]("Request arrived");
[Link]("Data arrived");
[Link]("Name : "+nn);
[Link]("Gender : "+ss);
[Link]("City : "+cc);
// some code to save data
[Link]("text/html");
PrintWriter pw;
pw=[Link]();
[Link]("<!Doctype html>");
[Link]("<html lang='en'>");
[Link]("<head>");
[Link]("<meta charset='utf-8'>");
[Link]("<title>[Link]</title>");
[Link]("</head>");
[Link]("<body>");
[Link]("<h1>Data Saved</h1></body></html>");
}catch(Exception exception)
{
[Link](exception);
}
}
}
The above code won't get compiled using
Thinking Machines – Java – J2EE Page 7

javac [Link]
use
javac -classpath c:\tomcat4\common\lib\[Link];. [Link]
Now start server, then browser as done earlier and type the following in the address bar

[Link]

you should see the following.


Thinking Machines – Java – J2EE Page 8

Now click the hyperlink with text Begin and you should see the following

Test the following scenarios

Click the save button without feeding anything, you should see the message box that says Name
required. Then Feed name, but type digits and click the save button and see what happens, you should
see a message appear that says invalid data, then correct it but don't select gender or city and click the
save button, now you should see a message that says, select gender, now select gender but don't select
city and click the save button, now you should see the message, select city. Now select city and click
the Save button, now the following should appear
Thinking Machines – Java – J2EE Page 9

And the following should appear on server window.

[Link]
[Link]

<!Doctype html>
<html lang='en'>
<head>
<meta charset='utf-8'>
<title>[Link]</title>
</head>
<body>
<center>
<h1><i>Welcome to a cool site</i></h1>
<h3>Ujjain</h3>
<h6><u>Madhya Pradesh</u></h6>
<b>India</b>
Thinking Machines – Java – J2EE Page 10

<br>
<br/>
<a href='/[Link]/[Link]'>Begin</a>
</center>
</body>
</html>
[Link]
<!Doctype html>
<html lang='en'>
<head>
<meta charset='utf-8'>
<title>[Link]</title>
<script>
function ramu(f)
{
var nn=[Link];
if([Link]==0)
{
alert('Name required'); [Link]();
return false;
}
var v='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz .';
[Link]("nameErrorSection").innerHTML="";
var e=0;
while(e<[Link])
{
if([Link]([Link](e))==-1)
{
[Link]("nameErrorSection").innerHTML="Invalid data in name";
[Link]();
return false;
}
e++;
}
if([Link][0].checked==false && [Link][1].checked==false)
{
alert("Select gender");
return false;
}
if([Link]==0)
{
alert("Select a city");
[Link]();
return false;
}
return true;
Thinking Machines – Java – J2EE Page 11

}
</script>
</head>
<body>
<center>
<h1>Session Tracking Example</h1>
<h4><u>Using Hidden Form Field</u></h4>
<h2>Personal Information - Page 1</h2>
<form action='/[Link]/servlet/aaa' onsubmit='return ramu(this)'>
<table border='0'>
<tr><td>
Name</td><td>
<input type='text' name='nm' id='nm' >
<span id='nameErrorSection'></span>
</td></tr><tr>
<td colspan='2' align='center'>
<input type='submit' value='Next'></td></tr></table>
</form>
</center>
</body>
</html>
[Link]
import [Link].*;
import [Link].*;
import [Link].*;
public class aaa extends HttpServlet
{
public void doGet(HttpServletRequest rq,HttpServletResponse rs)
{
try
{
String nn=[Link]("nm");
PrintWriter pw=[Link]();
[Link]("text/html");
[Link]("<!Doctype html>");
[Link]("<html lang='en'>");
[Link]("<head>");
[Link]("<meta charset='utf-8'>");
[Link]("<title>[Link]</title>");
[Link]("<script>");
[Link]("function ramu(f)");
[Link]("{");
[Link]("if([Link][0].checked==false && [Link][1].checked==false)");
[Link]("{");
[Link]("alert(\"Select gender\");");
[Link]("return false;");
Thinking Machines – Java – J2EE Page 12

[Link]("}");
[Link]("return true;");
[Link]("}");
[Link]("</script>");
[Link]("</head>");
[Link]("<body>");
[Link]("<center>");
[Link]("<h1>Session Tracking Example</h1>");
[Link]("<h4><u>Using Hidden Form Field</u></h4>");
[Link]("<h2>Personal Information - Page 2</h2>");
[Link]("Name : <b>"+nn+"</b></br>");
[Link]("<form action='/[Link]/servlet/bbb' onsubmit='return ramu(this)'>");
[Link]("<input type='hidden' name='nm' id='nm' value='"+nn+"'>");
[Link]("<table border='0'>");
[Link]("<tr><td>");
[Link]("Gender</td><td>");
[Link]("Male <input type='radio' name='sex' id='ml' value='M'>");
[Link]("&nbsp;&nbsp;&nbsp;");
[Link]("Female <input type='radio' name='sex' id='fe' value='F'>");
[Link]("</td></tr>");
[Link]("</tr><tr>");
[Link]("<td colspan='2' align='center'>");
[Link]("<input type='submit' value='Next'></td></tr></table>");
[Link]("</form>");
[Link]("</center>");
[Link]("</body>");
[Link]("</html>");

}catch(Exception e)
{
[Link](e);
}
}
}
[Link]
import [Link].*;
import [Link].*;
import [Link].*;
public class bbb extends HttpServlet
{
public void doGet(HttpServletRequest rq,HttpServletResponse rs)
{
try
{
String nn=[Link]("nm");
String ss=[Link]("sex");
Thinking Machines – Java – J2EE Page 13

PrintWriter pw=[Link]();
[Link]("text/html");
[Link]("<!Doctype html>");
[Link]("<html lang='en'>");
[Link]("<head>");
[Link]("<meta charset='utf-8'>");
[Link]("<title>[Link]</title>");
[Link]("<script>");
[Link]("function ramu(f)");
[Link]("{");
[Link]("if([Link]==0)");
[Link]("{");
[Link]("alert('Select a city');");
[Link]("[Link]();");
[Link]("return false;");
[Link]("}");
[Link]("return true;");
[Link]("}");
[Link]("</script>");
[Link]("</head>");
[Link]("<body>");
[Link]("<center>");
[Link]("<h1>Session Tracking Example</h1>");
[Link]("<h4><u>Using Hidden Form Field</u></h4>");
[Link]("<h2>Personal Information - Page 3</h2>");
[Link]("Name : <b>"+nn+"</b></br>");
[Link]("Sex : <b>"+ss+"</b></br>");
[Link]("<form action='/[Link]/servlet/ccc' onsubmit='return ramu(this)'>");
[Link]("<input type='hidden' name='nm' id='nm' value='"+nn+"'>");
[Link]("<input type='hidden' name='sex' id='sex' value='"+ss+"'>");
[Link]("<table>");
[Link]("<tr><td>");
[Link]("Select city</td><td>");
[Link]("<select name='ct' id='ct'>");
[Link]("<option value='-1'>&lt;Select&gt;</option>");
[Link]("<option value='101'>Pune</option>");
[Link]("<option value='102'>Ujjain</option>");
[Link]("<option value='103'>Indore</option>");
[Link]("</select></td></tr></tr><tr>");
[Link]("<td colspan='2' align='center'>");
[Link]("<input type='submit' value='Save'></td></tr></table>");
[Link]("</form>");
[Link]("</center>");
[Link]("</body>");
[Link]("</html>");
Thinking Machines – Java – J2EE Page 14

}catch(Exception e)
{
[Link](e);
}
}
}
[Link]
import [Link].*;
import [Link].*;
import [Link].*;
public class ccc extends HttpServlet
{
public void doGet(HttpServletRequest rq,HttpServletResponse rs)
{
try
{
String nn=[Link]("nm");
String ss=[Link]("sex");
String cc=[Link]("ct");
[Link]("Request arrived");
[Link]("Data arrived");
[Link]("Name : "+nn);
[Link]("Gender : "+ss);
[Link]("City : "+cc);
[Link]("text/html");
PrintWriter pw;
pw=[Link]();
// some code to save data
[Link]("<!Doctype html>");
[Link]("<html lang='en'>");
[Link]("<head>");
[Link]("<meta charset='utf-8'>");
[Link]("<title>[Link]</title>");
[Link]("</head>");
[Link]("<body>");
[Link]("<center>");
[Link]("<h1>Session Tracking Example</h1>");
[Link]("<h4><u>Using Hidden Form Field</u></h4>");
[Link]("<h1>Data Saved</h1></body></html>");
[Link]("Name : "+nn+"</br>");
[Link]("Gender : "+ss+"</br>");
[Link]("City : "+cc+"</br>");
[Link]("</center>");
}catch(Exception exception)
{
Thinking Machines – Java – J2EE Page 15

[Link](exception);
}
}
}
Now configure tomcat7 as we configured tomcat4. Finally you should have a shortcut to
c:\tomcat7\bin\[Link] on your desktop.

Note : in case of tomcat4 the [Link] is located in c:\tomcat4\common\lib


In case of tomcat7, the name of the jar file is [Link] and is located in tomcat7\lib folder.

Now move to c:\tomcat7\webapps\ and create the folder structure.

[Link]
[Link]\WEB-INF
[Link]\WEB-INF\classes\tm

[Link] (Location – [Link])


<!Doctype html>
<html lang='en'>
<head>
<meta charset='utf-8'>
<title>[Link]</title>
</head>
<body>
<center>
<h1><i>Welcome to a cool site</i></h1>
<h3>Ujjain</h3>
<h6><u>Madhya Pradesh</u></h6>
<b>India</b>
<br>
<br/>
<a href='/[Link]/[Link]'>Begin</a>
</center>
</body>
</html>
[Link]
<!Doctype html>
<html lang='en'>
<head>
<meta charset='utf-8'>
<title>[Link]</title>
<script>
function ramu(f)
{
var nn=[Link];
if([Link]==0)
Thinking Machines – Java – J2EE Page 16

{
alert('Name required'); [Link]();
return false;
}
var v='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz .';
[Link]("nameErrorSection").innerHTML="";
var e=0;
while(e<[Link])
{
if([Link]([Link](e))==-1)
{
[Link]("nameErrorSection").innerHTML="Invalid data in name";
[Link]();
return false;
}
e++;
}
if([Link][0].checked==false && [Link][1].checked==false)
{
alert("Select gender");
return false;
}
if([Link]==0)
{
alert("Select a city");
[Link]();
return false;
}
return true;
}
</script>
</head>
<body>
<center>
<h2>Personal Information</h2>
<form action='/[Link]/ProcessForm' onsubmit='return ramu(this)'>
<table border='1'>
<tr><td>
Name</td><td>
<input type='text' name='nm' id='nm' >
<span id='nameErrorSection'></span>
</td></tr><tr><td>
Gender</td><td>
Male <input type='radio' name='sex' id='ml' value='M'>
&nbsp;&nbsp;&nbsp;
Female <input type='radio' name='sex' id='fe' value='F'>
</td></tr><tr><td>
Thinking Machines – Java – J2EE Page 17

Select city</td><td>
<select name='ct' id='ct'>
<option value='-1'>&lt;Select&gt;</option>
<option value='101'>Pune</option>
<option value='102'>Ujjain</option>
<option value='103'>Indore</option>
</select></td></tr></tr><tr>
<td colspan='2' align='center'>
<input type='submit' value='Save'></td></tr></table>
</form>
</center>
</body>
</html>
[Link] (Location [Link]\WEB-INF\classes\tm)

To comple the following code, you will have to type


javac -classpath c:\tomcat7\lib\[Link];. *.java

package tm;
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
public class aaa extends HttpServlet
{
public void doGet(HttpServletRequest rq,HttpServletResponse rs)
{
try
{
String nn=[Link]("nm");
String ss=[Link]("sex");
String cc=[Link]("ct");
[Link]("Request arrived");
[Link]("Data arrived");
[Link]("Name : "+nn);
[Link]("Gender : "+ss);
[Link]("City : "+cc);
[Link]("text/html");
PrintWriter pw;
pw=[Link]();
[Link]("<!Doctype html>");
[Link]("<html lang='en'>");
[Link]("<head>");
[Link]("<meta charset='utf-8'>");
[Link]("<title>[Link]</title>");
Thinking Machines – Java – J2EE Page 18

[Link]("</head>");
[Link]("<body>");
[Link]("<center>");
[Link]("<h1>Session Tracking Example</h1>");
[Link]("<h4><u>Using URL Rewriting</u></h4>");
[Link]("<a href='/[Link]/SaveData?nm="+[Link](nn)
+"&ct="+[Link](cc)+"&sex="+[Link](ss)+"'>Save</a>");
[Link]("</center>");
[Link]("</body>");
[Link]("</html>");
}catch(Exception exception)
{
[Link](exception);
}
}
}
[Link] (Location [Link]\WEB-INF\classes\tm)

To comple the following code, you will have to type


javac -classpath c:\tomcat7\lib\[Link];. *.java

package tm;
import [Link].*;
import [Link].*;
import [Link].*;
public class bbb extends HttpServlet
{
public void doGet(HttpServletRequest rq,HttpServletResponse rs)
{
try
{
String nn=[Link]("nm");
String ss=[Link]("sex");
String cc=[Link]("ct");
[Link]("Request arrived");
[Link]("Data arrived");
[Link]("Name : "+nn);
[Link]("Gender : "+ss);
[Link]("City : "+cc);
[Link]("text/html");
PrintWriter pw;
pw=[Link]();
// some code to save data
[Link]("<!Doctype html>");
[Link]("<html lang='en'>");
[Link]("<head>");
Thinking Machines – Java – J2EE Page 19

[Link]("<meta charset='utf-8'>");
[Link]("<title>[Link]</title>");
[Link]("</head>");
[Link]("<body>");
[Link]("<center>");
[Link]("<h1>Session Tracking Example</h1>");
[Link]("<h4><u>Using URL Rewriting</u></h4>");
[Link]("<h1>Data Saved</h1></body></html>");
[Link]("Name : "+nn+"</br>");
[Link]("Gender : "+ss+"</br>");
[Link]("City : "+cc+"</br>");
[Link]("</center>");
}catch(Exception exception)
{
[Link](exception);
}
}
}
Note : copy paste the following in a file named as [Link]

[Link] (Location : [Link]\WEB-INF)


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="[Link]
xmlns:xsi="[Link]
xsi:schemaLocation="[Link] [Link]
app_2_5.xsd">
<servlet>
<servlet-name>FormProcessor</servlet-name>
<servlet-class>[Link]</servlet-class>
</servlet>
<servlet>
<servlet-name>DataSaver</servlet-name>
<servlet-class>[Link]</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FormProcessor</servlet-name>
<url-pattern>/ProcessForm</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>DataSaver</servlet-name>
<url-pattern>/SaveData</url-pattern>
</servlet-mapping>
</web-app>
[Link] (tomcat7\webapps)
Comple everything as done in [Link]
Thinking Machines – Java – J2EE Page 20

[Link]

<!Doctype html>
<html lang='en'>
<head>
<meta charset='utf-8'>
<title>[Link]</title>
</head>
<body>
<center>
<h1><i>Welcome to a cool site</i></h1>
<h3>Ujjain</h3>
<h6><u>Madhya Pradesh</u></h6>
<b>India</b>
<br>
<br/>
<a href='/[Link]/[Link]'>Begin</a>
</center>
</body>
</html>
[Link]
<!Doctype html>
<html lang='en'>
<head>
<meta charset='utf-8'>
<title>[Link]</title>
<script>
function ramu(f)
{
var nn=[Link];
if([Link]==0)
{
alert('Name required'); [Link]();
return false;
}
var v='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz .';
[Link]("nameErrorSection").innerHTML="";
var e=0;
while(e<[Link])
{
if([Link]([Link](e))==-1)
{
[Link]("nameErrorSection").innerHTML="Invalid data in name";
[Link]();
return false;
}
Thinking Machines – Java – J2EE Page 21

e++;
}
if([Link][0].checked==false && [Link][1].checked==false)
{
alert("Select gender");
return false;
}
if([Link]==0)
{
alert("Select a city");
[Link]();
return false;
}
return true;
}
</script>
</head>
<body>
<center>
<h2>Personal Information</h2>
<form action='/[Link]/ProcessForm' onsubmit='return ramu(this)'>
<table border='1'>
<tr><td>
Name</td><td>
<input type='text' name='nm' id='nm' >
<span id='nameErrorSection'></span>
</td></tr><tr><td>
Gender</td><td>
Male <input type='radio' name='sex' id='ml' value='M'>
&nbsp;&nbsp;&nbsp;
Female <input type='radio' name='sex' id='fe' value='F'>
</td></tr><tr><td>
Select city</td><td>
<select name='ct' id='ct'>
<option value='-1'>&lt;Select&gt;</option>
<option value='101'>Pune</option>
<option value='102'>Ujjain</option>
<option value='103'>Indore</option>
</select></td></tr></tr><tr>
<td colspan='2' align='center'>
<input type='submit' value='Save'></td></tr></table>
</form>
</center>
</body>
</html>
Thinking Machines – Java – J2EE Page 22

[Link]
package tm;
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
public class aaa extends HttpServlet
{
public void doGet(HttpServletRequest rq,HttpServletResponse rs)
{
try
{
String nn=[Link]("nm");
String ss=[Link]("sex");
String cc=[Link]("ct");
Cookie c1=new Cookie("nm",nn);
Cookie c2=new Cookie("ct",cc);
Cookie c3=new Cookie("sex",ss);
[Link](c1);
[Link](c2);
[Link](c3);
[Link]("Request arrived");
[Link]("Data arrived");
[Link]("Name : "+nn);
[Link]("Gender : "+ss);
[Link]("City : "+cc);
[Link]("text/html");
PrintWriter pw;
pw=[Link]();
[Link]("<!Doctype html>");
[Link]("<html lang='en'>");
[Link]("<head>");
[Link]("<meta charset='utf-8'>");
[Link]("<title>[Link]</title>");
[Link]("</head>");
[Link]("<body>");
[Link]("<center>");
[Link]("<h1>Session Tracking Example</h1>");
[Link]("<h4><u>Using Cookies</u></h4>");
[Link]("<a href='/[Link]/SaveData'>Save</a>");
[Link]("</center>");
[Link]("</body>");
[Link]("</html>");
}catch(Exception exception)
{
[Link](exception);
}
Thinking Machines – Java – J2EE Page 23

}
}
[Link]
package tm;
import [Link].*;
import [Link].*;
import [Link].*;
public class bbb extends HttpServlet
{
public void doGet(HttpServletRequest rq,HttpServletResponse rs)
{
try
{
Cookie c[]=[Link]();
String nn="";
String ss="";
String cc="";
if(c!=null)
{
for(Cookie r:c)
{
if([Link]().equals("nm"))
{
nn=[Link]();
} else if([Link]().equals("ct"))
{
cc=[Link]();
} else if([Link]().equals("sex"))
{
ss=[Link]();
}
}
}
[Link]("Request arrived");
[Link]("Data arrived");
[Link]("Name : "+nn);
[Link]("Gender : "+ss);
[Link]("City : "+cc);
[Link]("text/html");
PrintWriter pw;
pw=[Link]();
[Link]("<!Doctype html>");
[Link]("<html lang='en'>");
[Link]("<head>");
[Link]("<meta charset='utf-8'>");
[Link]("<title>[Link]</title>");
Thinking Machines – Java – J2EE Page 24

[Link]("</head>");
[Link]("<body>");
[Link]("<center>");
[Link]("<h1>Session Tracking Example</h1>");
[Link]("<h4><u>Using Cookies</u></h4>");
[Link]("<h1>Data Saved</h1></body></html>");
[Link]("Name : "+nn+"</br>");
[Link]("Gender : "+ss+"</br>");
[Link]("City : "+cc+"</br>");
[Link]("</center>");
}catch(Exception exception)
{
[Link](exception);
}
}
}
[Link] (Location [Link]\WEB-INF)
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="[Link]
xmlns:xsi="[Link]
xsi:schemaLocation="[Link] [Link]
app_2_5.xsd">
<servlet>
<servlet-name>FormProcessor</servlet-name>
<servlet-class>[Link]</servlet-class>
</servlet>
<servlet>
<servlet-name>DataSaver</servlet-name>
<servlet-class>[Link]</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FormProcessor</servlet-name>
<url-pattern>/ProcessForm</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>DataSaver</servlet-name>
<url-pattern>/SaveData</url-pattern>
</servlet-mapping>
</web-app>
[Link]
Compile everything as done in [Link]
[Link]
<!Doctype html>
<html lang='en'>
<head>
<meta charset='utf-8'>
Thinking Machines – Java – J2EE Page 25

<title>[Link]</title>
</head>
<body>
<center>
<h1><i>Welcome to a cool site</i></h1>
<h3>Ujjain</h3>
<h6><u>Madhya Pradesh</u></h6>
<b>India</b>
<br>
<br/>
<a href='/[Link]/[Link]'>Begin</a>
</center>
</body>
</html>
[Link]
<!Doctype html>
<html lang='en'>
<head>
<meta charset='utf-8'>
<title>[Link]</title>
<script>
function ramu(f)
{
var nn=[Link];
if([Link]==0)
{
alert('Name required'); [Link]();
return false;
}
var v='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz .';
[Link]("nameErrorSection").innerHTML="";
var e=0;
while(e<[Link])
{
if([Link]([Link](e))==-1)
{
[Link]("nameErrorSection").innerHTML="Invalid data in name";
[Link]();
return false;
}
e++;
}
if([Link][0].checked==false && [Link][1].checked==false)
{
alert("Select gender");
return false;
Thinking Machines – Java – J2EE Page 26

}
if([Link]==0)
{
alert("Select a city");
[Link]();
return false;
}
return true;
}
</script>
</head>
<body>
<center>
<h2>Personal Information</h2>
<form action='/[Link]/ProcessForm' onsubmit='return ramu(this)'>
<table border='1'>
<tr><td>
Name</td><td>
<input type='text' name='nm' id='nm' >
<span id='nameErrorSection'></span>
</td></tr><tr><td>
Gender</td><td>
Male <input type='radio' name='sex' id='ml' value='M'>
&nbsp;&nbsp;&nbsp;
Female <input type='radio' name='sex' id='fe' value='F'>
</td></tr><tr><td>
Select city</td><td>
<select name='ct' id='ct'>
<option value='-1'>&lt;Select&gt;</option>
<option value='101'>Pune</option>
<option value='102'>Ujjain</option>
<option value='103'>Indore</option>
</select></td></tr></tr><tr>
<td colspan='2' align='center'>
<input type='submit' value='Save'></td></tr></table>
</form>
</center>
</body>
</html>
[Link]
package tm;
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
public class aaa extends HttpServlet
Thinking Machines – Java – J2EE Page 27

{
public void doGet(HttpServletRequest rq,HttpServletResponse rs)
{
try
{
String nn=[Link]("nm");
String ss=[Link]("sex");
String cc=[Link]("ct");
HttpSession sess=[Link](true);
[Link]("nm",nn);
[Link]("ct",cc);
[Link]("sex",ss);
[Link]("Request arrived");
[Link]("Data arrived");
[Link]("Name : "+nn);
[Link]("Gender : "+ss);
[Link]("City : "+cc);
[Link]("text/html");
PrintWriter pw;
pw=[Link]();
[Link]("<!Doctype html>");
[Link]("<html lang='en'>");
[Link]("<head>");
[Link]("<meta charset='utf-8'>");
[Link]("<title>[Link]</title>");
[Link]("</head>");
[Link]("<body>");
[Link]("<center>");
[Link]("<h1>Session Tracking Example</h1>");
[Link]("<h4><u>Using Session Object</u></h4>");
[Link]("<a href='/[Link]/SaveData'>Save</a>");
[Link]("</center>");
[Link]("</body>");
[Link]("</html>");
}catch(Exception exception)
{
[Link](exception);
}
}
}
[Link]
package tm;
import [Link].*;
import [Link].*;
import [Link].*;
public class bbb extends HttpServlet
Thinking Machines – Java – J2EE Page 28

{
public void doGet(HttpServletRequest rq,HttpServletResponse rs)
{
try
{
String nn="";
String ss="";
String cc="";
HttpSession sess;
sess=[Link](false);
if(sess!=null)
{
nn=(String)[Link]("nm");
cc=(String)[Link]("ct");
ss=(String)[Link]("sex");
}
[Link]("Request arrived");
[Link]("Data arrived");
[Link]("Name : "+nn);
[Link]("Gender : "+ss);
[Link]("City : "+cc);
[Link]("text/html");
PrintWriter pw;
pw=[Link]();
[Link]("<!Doctype html>");
[Link]("<html lang='en'>");
[Link]("<head>");
[Link]("<meta charset='utf-8'>");
[Link]("<title>[Link]</title>");
[Link]("</head>");
[Link]("<body>");
[Link]("<center>");
[Link]("<h1>Session Tracking Example</h1>");
[Link]("<h4><u>Using Session Object</u></h4>");
[Link]("<h1>Data Saved</h1></body></html>");
[Link]("Name : "+nn+"</br>");
[Link]("Gender : "+ss+"</br>");
[Link]("City : "+cc+"</br>");
[Link]("</center>");
}catch(Exception exception)
{
[Link](exception);
}
}
}

Common questions

Powered by AI

Utilizing older versions of Java and Tomcat, as indicated by the document, presents challenges such as lack of support for modern security standards, limited functionality in handling updated technologies, and potential compatibility issues with newer software components. Developers might face difficulties in integrating with services that demand higher compliance levels or in applying patches for vulnerabilities. Additionally, managing deprecated features and aligning configurations between outdated libraries and newer application requirements adds complexity, impacting development and deployment efficiency .

Key methods for session tracking in Java-based web applications include hidden form fields, URL rewriting, cookies, and the use of the session object. Hidden form fields involve passing data along with the form submission, URL rewriting appends session information to URLs, cookies store user data on the client-side, and the session object stores data on the server-side. Each method has different implications for data accessibility, security, and performance. For example, using cookies involves minimal server resource usage but poses greater security risks if not handled properly .

'web.xml' plays a crucial role in configuring servlets by acting as a deployment descriptor, defining servlet classes and mapping them to specific URLs through <servlet> and <servlet-mapping> elements. This configuration enables the server to recognize how URL patterns relate to particular servlets that handle requests. In the document, 'web.xml' specifies servlet names and classes, mapping them to actions like '/ProcessForm' and '/SaveData', ensuring that form submissions and data saving are routed to their respective servlets .

URL rewriting for session tracking involves appending session data as query parameters to the URLs. In the document, session data such as name, gender, and city is appended to a URL when a user interacts with the application. This is demonstrated in a servlet where data is encoded using URLEncoder and included in a hyperlink, allowing the session information to be passed along with the request to the server. This method is beneficial for clients that do not support cookies but can result in less clean URLs .

The primary purpose of configuring the Tomcat server in a Java web application is to create a local environment where the web application can be deployed and tested efficiently. According to the document, it is achieved by downloading Tomcat, unzipping it to a specified directory such as c:\tomcat4, editing startup.bat to set the CATALINA_HOME and JAVA_HOME paths, and starting the server using a shortcut to the startup.bat script. This configuration allows developers to access the application via http://localhost:8080 .

In the discussed web applications, servlets are integrated to handle form submissions by acting as server-side programs that process HTTP requests received from client-side forms. For instance, when a form is submitted, the data is retrieved on the server using HttpServletRequest methods, and a servlet class processes this data, potentially storing or manipulating it before generating a response. The document illustrates this with Java servlet classes like aaa and bbb, which retrieve form parameters, process them, and send back an HTML response that informs the user of successful data handling .

Switching from Tomcat 4 to Tomcat 7 offers several advantages, such as improved performance, enhanced security features, and support for the latest Java EE specifications, which results in better handling of modern web applications. However, it requires careful migration of existing applications, as changes like updated library paths (servlet-api.jar instead of servlet.jar) necessitate modifications in configurations and compilation commands. While upgrading boosts overall application capabilities and security, it involves effort in terms of reconfiguration and potential changes in application code to comply with new features and deprecations .

The document illustrates form input validation using JavaScript by employing functions that check whether required fields are filled and whether inputs match expected formats. For example, the function 'ramu' checks for a non-empty name field, uses a character list to ensure values contain only allowed characters, and confirms that radio buttons (for gender) are selected and that a valid option (city) is picked from a dropdown. Alerts and focus shifts are employed to guide user correction upon invalid input .

Proper coding practices in servlet development are crucial in avoiding exceptions and ensuring smooth execution. As indicated in the document, catching and handling exceptions properly prevents application crashes and aids in debugging. For example, using try-catch blocks around input retrieval and response writing protects against runtime errors and provides meaningful logging through standard output streams, allowing developers to diagnose issues without interrupting service availability .

The main security concerns with session tracking methods, particularly cookies and URL rewriting, include interception of session data, replay attacks, and exposure of sensitive information. Cookies stored on the client-side are vulnerable to attacks like cross-site scripting if not secured properly, as they can be easily accessed by malicious scripts. URL rewriting leads to session information appearing in URLs, which can be intercepted through network traffic analysis or browser logs. Proper use of HTTPS, cookie flags (like HttpOnly and Secure), and encryption of session data can mitigate these risks .

You might also like