How Servlet container could have implemented service method ?

I want to know for my knowledge, Does jsp container or servlet container implement any class that in turn calls the service method of our class passing them request and response object. As request and response are the interfaces, they only define certain methods leaving implementation of these methods for the container. My question is how they might be implementing those methods and then calling the service method passing them those objects. I will print one example that i created so that you guys will have some idea of my query.

// Take this as the HttpServletRequest interface. Reference of this interface should be passed

//to the user-defined class's method.

interface ServletRequestInterface

{

public String getName();

}

// The class implementing the above interface

class Test implements ServletRequestInterface

{

ServletRequestInterface a=null;

public String getName(){

return "Hi";

}

Test(){setServletRequestInterface();}

public void setServletRequestInterface()

{

a= this;

}

public ServletRequestInterface getServletRequestInterface()

{

return a;

}

}

//the class actually making use of the above interface but still it is not ready to pass it to some user

//defined method. How can we do it or is probably handled in container.

public class ServletContainer

{

public static void main(String args[])

{

Test t = new Test();

System.out.println(t.getServletRequestInterface().getName());

}

}

This will print hi. Now if i want to pass this ServletRequestInterface to user-defined methods, how will i pass it automatically.

[1751 byte] By [joshi_tarak] at [2007-9-19]
# 1

Hi,

I will concentrate on the main class and give you the flow,since I had worked on designing the servlet engine (prelimaniory stage ) of my own

>>public class ServletContainer

>>{

>>public static void main(String args[])

>>{

>>Test t = new Test();

>>System.out.println(t.getServletRequestInterface().getName());

>>}

>>}

Follow these steps(Servlet Container)

1)Once the request is reached to the socket do the processing

2)the processing involves to detect the type of request,whether the request is for the servlet or

html

3)Below I am giving a simple example which can send you the html out put at port no 2323,You

require to work a lot if you really want to create an container It is not difficult but requires time

and the concepts clear.

4)If the request is send for the servlet then load the servlet from the default location,also check if

it is already loaded in jvm.I had written some logic ,hope u can understand it (It is not working)

5)If it is html read the html file and send the response from the output stream,quite simple.

Just set the classpath and run the following app by typing

http://localhost:2323/

Although I have not answered your query and hope this will give you some idea of servlet engine.

Hey to create a servlet container we have to write the java classes and follow the servlet specification,the core java has to be strong.

CODE

*******************

import java.net.*;

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

/**

* A Class class.

* <P>

* @author Vicky

*/

public class myEngine extends Object {

/**

* Constructor

*/

public myEngine() {

System.out.println("start cons");

//oconf=new EngineConfig();

System.out.println("end cons");

}

public static void main(String args[])

{

ServerSocket os=null;

//EngineConfig oconff=new EngineConfig();

//System.out.println(oconff.getServletName());

//System.out.println("conf "+oconff);

while(true)

{

try

{

if(os==null)

os=new ServerSocket(2323);

System.out.print("hello first1");

//Loading the Servlet

//Servlet oser=(Servlet)Class.forName("fes.DisplayServlet");

System.out.println("before");

Class c=ClassLoader.getSystemClassLoader().loadClass("DisplayServlet");

System.out.println(c);

System.out.println("Is interface "+c.isInterface());

HttpServlet oser=(HttpServlet)c.newInstance();

System.out.println("Problem before config");

//oser.init(oconff);

System.out.println("oser value "+oser);

ServletConfig oconf=oser.getServletConfig();

System.out.println("info of servlet "+oser.getServletInfo());

//why oconf is null?

System.out.println("config "+oconf);

//Class.forName("DisplayServlet").getClassLoader().loadClass("DisplayServlet");

System.out.print("after");

Socket oskt=os.accept();

System.out.print("hello");

InetAddress iadd=os.getInetAddress();

//ServletConfig oconfig=oser.getServletConfig();

//System.out.println("servlet which is loaded "+oconfig.getServletName());

InputStream in=oskt.getInputStream();

OutputStream o=oskt.getOutputStream();

String s="hello from port no"+"<font color=red size=10>2323</font>";

o.write(s.getBytes());

System.out.println("available "+in.available());

System.out.println("before datastream");

DataInputStream oda=new DataInputStream(in);

BufferedReader obr=new BufferedReader(new InputStreamReader(in));

System.out.println("after datastream");

String sdata="";

//boolean b=true;

try

{

// while(b)

// {

String sss=obr.readLine();

if(sss!=null)

sdata=sdata+sss;

// }

System.out.println(sdata);

}

catch(Exception e1)

{

System.out.println(e1);

}

System.out.println("FROM INPUT STREAM "+sdata);

//From the sdata we get the request parameter passed from the client to the server

System.out.print("Inetaddress of server being "+iadd);

obr.close();

oskt.close();

// o.close();

}

catch(Exception e)

{

System.out.println("here "+e);

}

}

}

}

*************************************

regards vicky

vickyk at 2007-7-5 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 2
thank you vicky, I think this will help me to resolve my problem, but tell me how to run this example exactly. Thanks once again for your valueable reply.
joshi_tarak at 2007-7-5 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 3

Hi Tarak,

Follow the following steps:

1)Copy the code and save it in a file called myEngine.java

2)set appropriate classpath ,as it reqiures the ServletContext and so on.As I already stated I

didnot get time to do this so this is just at the beginning stage,infact you can remove all the servlet related classes.

3)Compile the program

javac myEngine.java

4)Run this now

java myEngine

5)Open the browser and type http://localhost:2323/ from the same machine,and if you are trying it

from the different machine type http://IPADDRESS:2323/

6)See the output and let me know

What I expect from you is a level ahead,yes start working to generate the servlet engine,If you face

any problems ask me.

Try this now and let me know!!!!!!!!!1

regards vicky

vickyk at 2007-7-5 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 4

hi vicky thanks for your help. It really worked we just had to change the port number and also had to comment the DisplayServlet related lines. It prints

hello from port no3001

Anyways, we will have to go through your code and at the moment we will have to clear few concepts as is apparant from your code. So we will start working on it and will be definitely in touch with you. We would really love to receive your help when we start to dig in these concepts more deeply. We are working so it is difficult to learn this things very fast. It will take time and we will definitely keep u updated and also try and ask few queries. I hope you won't mind.

joshi_tarak at 2007-7-5 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 5

Hi,

Great to know that it had helped you,Actually long back in the forums I had invited the developers(precisely amatures) to share there thoughts for development of servlet Container but nobody showed interest.But that didnot stopped me I am working on the design of it and once I finish the implementation at basic level I will share that with all amatures.The main problem in developing the container is you require the ample of time,as companies creating the professional containers had lots of professionals working on those giant projects

Good Luck !!!

regards vicky

vickyk at 2007-7-5 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 6
Hi vicky,ur input is really valueable. I am studying it.thanxbye,Samir
samirkolarkar at 2007-7-5 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 7

Hi vicky,

I am back after sat,sun break . Here i present some code which originally reflect,

our problem of how container could have called service method ?

This is a very simple example (not http based but look for logic)

It execute as follows ,

when you run JspPageClass,

it prompts

Enter request name :

// enter test here.

Enter request value:

// enter any value here.

and the result will be,

Inside Service Method request.getParameter("test")==>// request value you entered.

so go through this examples and think how service method could have called.

Ok give me reply...

Examples.

1)

/* Consider this class as input taker and generate action though it takes value from,

* commandprompt just see how it works.

*/

package myjava.servlet.command;

import java.io.*;

public class JspPageClass

{

public static void main(String args[])

{

try

{

System.out.println("Enter request name :");

BufferedReader br1 = new BufferedReader (new InputStreamReader(System.in));

String s1 = br1.readLine();

System.out.println("Enter request value :");

BufferedReader br2 = new BufferedReader (new InputStreamReader(System.in));

String s2 = br2.readLine();

if(s1!=null && s2!=null)

{

ServletContainer sc = new ServletContainer();

sc.startService(s1,s2);

}

}

catch(IOException e)

{

System.out.println("Error getting details..."+e);

}

}

}

2)

/* Consider this classes where actual process happened.like setting of name value pair etc...

* And from here service method of our servlet called.

*/

package myjava.servlet.command;

class Test implements ServletRequestInterface{

String name=null,value=null;

ServletRequestInterface a=null;

public String getParameter(String s){

if(s!=null && s.equals(name))

return value;

else

return null;

}

Test(String s1,String s2){

name = s1;

value = s2;

setServletRequestInterface();

}

public void setServletRequestInterface()

{

a= this;

}

public ServletRequestInterface getServletRequestInterface()

{

return a;

}

}

public class ServletContainer

{

public void startService(String name,String param)

{

Test t = new Test(name,param);

System.out.println("Calling Service .......");

myjava.servlet.command.MyServlet ms = new myjava.servlet.command.MyServlet();

ms.service(t);

}

}

3)

/* Consider this as our ServletRequest interface that contains getParameter method.

*/

package myjava.servlet.command;

public interface ServletRequestInterface{

public String getParameter(String s);

}

4)

/* Consider this as our servlet .service method of this servlet is called by the ,

* container.This is a very simple example do not think of response now.

*/

package myjava.servlet.command;

public class MyServlet

{

protected void service(ServletRequestInterface request)

{

System.out.println("Inside Service Method request.getParameter(\"test\")==>"+request.getParameter("test"));

}

}

joshi_tarak at 2007-7-5 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 8

Hi joshi,

Well I just scanned the code and could make that you are creating this as per your specification,that is fine for the beginning.What I fell you should understand is

1)The definition of the ServletContainer should be like this

ServerSocket ss=new ServerSocket(portno)

while(true)

{

Socket request=socket.accept();

new HandlerRequest(request).start();

}

The HandlerRequest thread should take the parameter as socket which contains the InputStream and the OutputStream.

The Logic for this HandlerRequest run() should go as:

1)It should take the servlet name requested.For the first invocation the servlet is not loaded in the

JVM so Load the servlet in JVM(Refer to the ClassLoader in java.lang.*).Once it is loaded then it

should be stored in the list saying that the object by the servlet name is loaded.Now this storage

is similar to the ServletContext.

For the next invocation you should first check in the context whether the Servlet is available or not.In fact the ServletContext or your own context object should be instantiated first ie before the

ServletContainer.

In the Context Object you can define the info of the Servlet:

a)What is the servlet name

b)What time it is loaded in the JVM

etc etc

So you can create an ContextInfo class and set the properties and then store that in the HashTable which will be checked by every Request.

2)

And the HandlerRequest should flow like this(Prelimanary stage)

Class HandlerRequest extends Thread

{

As this Thread is getting the Socket Object so

1)Construct the InputStream from the Socket

2)Construct the OutputStream from the Socket

3)Get the ContextObject and get the HashTable which carries the names of the servlet loaded

in the jvm

4)Once you get the entry with the same servet name then check if the java file is latest or the class file

5) If the class file is latest then call the service method of the Servlet

else

load it back

......................................................... and it will follow all the specifications

Any way it seems the way you are doing it is fine but before starting this remember you should have designed this properly.You take time in designing,coding is then iota of work!!!!!!!!!!!!!!!!!!!!

vickyk at 2007-7-5 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 9
Thanks vicky for your reply.I will gradually move towards the points you have made here.thanks once again. keep in touch.
joshi_tarak at 2007-7-5 > top of java,Enterprise & Remote Computing,Web Tier APIs...