Thursday, November 8, 2012

MVC & J2EE

  • Model- View - Controller
With MVC the business logic is not only separate from the presentation... it doesn't even know what is a presentation.


MVC takes the business logic out of the servlet, and puts it in a "Model" - a reusable plain old Java class. The Model is a combination of the business data (like the state of a Shopping Cart) and the methods (rules) that operate on that data. 


How J2EE fits into this?

A fully-compliant J2EE application server must have both a web Container and an EJB Container.
Tomcat is just a web Container!
EJB: Enterprise JavaBeans


Apache: HTTP Web Server
Tomcat: Web Container

  • Use a DD (Deployment Descriptor) to Invoke a Servlet
Servlet Name: HelloWorldServlet.java
To invoke a servlet directly (EX: http://localhost:8080/FirstServlet/HelloWorldServlet), you have to add the following code into Apache-Tomcat/conf/web.xml
<servlet-mapping>
        <servlet-name>invoker</servlet-name>
        <url-pattern>/servlet/*</url-pattern>
</servlet-mapping>

But this is not safe, user know you file name if so.
The much better way of invoking a servlet is by providing an explicit mapping for it. This is accomplished by using a pair of tags in your web application's web.xml file. This is not the same file as mentioned above. This web.xml file (which you will probably need to create) will reside in your web-application's WEB-INF/ directory. For each servlet you want to call, provide a pair of tags like the following:
This XML file is the Deployment Descriptor
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  id="WebApp_ID" version="3.0">
<display-name>FirstServlet</display-name>
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>com.theopentutorials.servlets.HelloWorldServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/hello</url-pattern> 
</servlet-mapping>
</web-app>

Order of elements in web.xml is very important. So when you go to create your second 'couplet', make sure all your <servlet> tags are declared before your <servlet-mapping> tags. 



No comments:

Post a Comment