Friday, 16 August 2013

writing a small Web application

How to write web application? using Eclipse IDE

developing the first servlet program

1. creating the home.html page
2. creating the lifycycleservlet
3. creating deployment descriptor
4. deploying the application
5. running the application


·         Now we are going to write one servlet program
First stop the tomcat server
·         Right click on tomcat button, choose stop option
·         Now we will get like below

Now I want to clear the logs that is place the cursor on logs (text (red color) and right click the mouse , clear.
·         Go to project explorer tab, right click
Select new->other->press enter
·         Here you will have project templates, you will see only if you installed eclipse java ee developer
·         Go to WEB folder->dynamic web project
·         Click on Next button

·         Give any name for project name: I will give “simpleservletproject”, and use the default location that is workspace location, and select the tomcat server (no need to worry by default it will be selected), configuration will be the same tomcat configuration. Every thing I kept default( I didn’t change )

·         Click on next
·         Select scr folder


·         Click on next. I kept all are default (after selecting scr folder)
·         Context root ( I will explain you later this one) right now I want to keep it default
·          
·         And enable generate web.xml deployment descriptor
·         And click finish
·         Now it will display one small dialog box(in some systems) , click on yes
·         Now select the server tab
·         Select deployment descriptor from left hand side folder like below diagram



·         Go to window bar and choose show view->navigator

·         This is how we are organizing the file system, .class path and metadata for eclipse
·         Expand the webContent folder from left side->web
·         Just double click on web.xml file
·         It shows the xml view
·         Click on source tab

·         This is web.xml file,
What is WebContent file? It contains the web files like html, jsp, javascript files
·         Go to WebContent->right click->new->other->
·         Click on html file and click on next
·          
·          give the file name(ex: index.html

·         Click on Next




·         Click on finish button

·         Write the name of the web application at title tag
·         You can write any text message in the body

·         We have web.xml file which actually tells web server
·         You can remove all the tags in the web.xml file
Now go to simpleservletproject folder leftside of the explorer

·         Right click->Run As->Run on Server


·         Click on ok button
·         Which server you want , select  i.e Tomcat server
·         Click on next
·         Select the configuration project
·         Click on finish button
·         Inside eclipse browser tab will be opened and executed the web application
·         Whatever we typed in index.html that is displayed


USING NOTE PAD:



No we will talk about the context











1. creating the home.html page
2. creating the lifycycleservlet
3. creating deployment descriptor
4. deploying the application
5. running te application

1st step:
<html>
 <head>
  <title servlet example </title>
 </head>
 <body>
 <form action="myfirstServlet"><br/><br/><br/>
 <center>
 <input type="submit" value="invoke servlet lifecycle"/>
 </center>
  </form>
</body>
</html>

2. creating the lifycycleservlet

package com.rajendra.servlets;
import javax.servlet.*;
public class LifeCycleServlet implements Servlet
{
 public void init(ServletConfig sc)
 {
  System.out.println("init() method");
 }
 public void service(ServletRequest req, ServletResponse res) throws ServletException, java.io.IOException

 {
  java.io.PrintWriter out=res.getWriter();
  out.println("hi frm lifecycleservlet");
  System.out.println("in service");
 }
 public void destroy()
 {
  System.out.println("in destroy()")
 }
 public String getServletInfo()
 {
  return "LifeCycleServlet";
 }
 public ServletConfig getServletConfig()
 {
  return config;
 }
private ServletConfig config;
}

creating the deployment descriptor























No comments:

Post a Comment