Sunday, May 6, 2012

Now I know JSP and Servlet can work together

I am going back to the basic. In web development, JAVA code and JSP code are two different thing. Then how JSP code and JAVA code link up together?

The picture above showing Servlet and JSP as separate entity

In order to link them up, we need web.xml. We tell the server once we receive an URL request that look like this:
http://< ip >:< port >/< project >/< path >
The Servlet object will be initialize and do its job. This is how we should instruct the servlet to do its job in the web.xml.
< servlet >
   < servlet-name >My Servlet< /servlet-name >
   < servlet-class >org.huahsin68.MyServlet< /servlet-class >
< /servlet >
< servlet-mapping >
   < servlet-name>My Servlet< /servlet-name >
   < url-pattern>/MyPath/*< /url-pattern>
< /servlet-mapping >
When someone enter http://< ip >:< port >/MyPath. MyServlet object will get execute, and process client's request in doGet() function. In doGet(), the JSP Page will get forward using following code:

req.getRequestDispatcher("/thePage.jsp").forward(req, res);

So, is this basic enough?

No comments: