how can I run java servlet thread safety program using tomcat server? please give me a step by step procedure to run the following program
my program is
A DEMO PROGRAM FOR THREAD SAFETY.
package serv;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
class account
{
public int bal;
}
public class threadsfty extends HttpServlet
{
account acct;
public void init(ServletConfig config)throws ServletException
{
super.init(config);
acct=new account();
try{
File r=new File(?count?);
DataInputStream d=new DataInputStream(new FileInputStream(r));
acct.bal=d.readInt();
}
catch(FileNotFoundException e)
{
System.out.println(?error?+e);
}
catch(IOException e)
{
System.out.println(?error?+e);
}
}
public void doGet(HttpServletRequest req,HttpServletResponseres)throws ServletException,IOException
{
res.setContentType(?text/html?);
printWriter out=res.getWriter();
out.println(?<html><body bgcolor=pink><center>?);
out.println(?<h2>ATM</h2>?);
out.println(?current balance:?+acct.bal);
out.println(?<from method=POST action=\?http://localhost:8080/examples/servlet/serv.threadsfty\?>?);
out.println(?<input type=submit name=deposit values=deposit>?);
out.println(?<input type=submit name=withdraw value=withdraw>?);
out.println(?</form></body></html>?);
}
Public void doPost(HttpServletRequest req.HttpServletResposeres)throws ServletException,IOException
{
int amt=0;
try{
amt=Integer.parseInt(?amount?);
}
catch(NullPointerException e)
{
}
catch(NumberFormatException e)
{
}
synchronized(acct)
{
amt=Integer.parseInt(req.getParameter(?amount?));
res.setContenttype(?text/html?):
PrintWriter out=res.getWriter();
if(req.getParameter(?withdraw?)!=null)
{
if(amt>acct.bal)
out.println(?soory?);
else
{
acct.bal-=amt;
out.println(?withdraw success?);
}
if(req.getParameter(?deposit?)!=null)&&(amt.0))
{
acct.bal+=amt;
out.println(?deposit success?);
}
}
doGet(req.res);
}
public void destroy()
{
File r=new File(?counter?);
try{
DataOutputStream dout=new DataOutputStream(new FileOutputStream(r));
dout.writeInt(acct.bal);
}
catch(IOException e)
{
System.out.println(?erroe?+e);
}
}
}
Put servlet-api.jar inside the lib folder of apache tomcat.
1)create your servlet 'threadsfty'.
2)Go to the webapps folder of your apache tomcat and create a web application folder but it should having an appropriate name like examples.
3)Create web.xml and classes folder inside the WEB_INF folder of web application folder.
4)Copy the servlet to the classes folder.
5)Edit the web.xml to include servlet?s name and url pattern.
<servlet> <servlet-name>threadsfty</servlet-name> <servlet-class>threadsfty </servlet-class> </servlet> <servlet-mapping> <servlet-name>threadsfty</servlet-name> <url-pattern>/threadsfty</url-pattern> </servlet-mapping>
6)Compile your servlet.
7)Run Tomcat server by clicking the startup.bat file. This is located inside the bin folder of apache tomcat.
8)Open the browser and type the following url:
http://localhost:8080/webapplicationfolder_name/threadsfty
For more information, visit the following link: