otak info official Logo   
OTAK.INFO
Personal Blog

Hariyanto Lim

정길상 / 鄭吉祥
Home   Last Posts  
 
Please login to see more topics and discussion categories.
 
goto main category list >> Software programming >> ServLet

Title : Create and define ServLet
Total Reply : 0
Total View : 2777


Harry
Harry
Total thread: 131
Total reply: 50

Post #118
Create and define ServLet
To create a new ServLet, we create a new Java class such as
'
'.str_replace('
', ' ', '
public class MyClass extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {

// ... do some logic here

}
}
').'
'

After create the .java code for the ServLet, then we must define it in web.xml
'
'.str_replace('
', ' ', '
<?xml version="1.0" encoding="utf-8"?>
<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_2_5.xsd" version="2.5">

<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>com.mydomain.mypackage.MyServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/MyServletURLpath</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
').'
'

In the above web.xml example, we define our ServLet name as "MyServlet", the class path is "com.mydomain.mypackage.MyServLet" and then we define the servlet URL mapping using <url-pattern> to a url path "/MyServletURLpath", so when we execute this servlet in our local server we can call this servlet using "http://localhost/MyServletURLpath"
Falling in love with the world

Write : 2013-07-18 11:30:53

If you want to create a new reply then please login first.



www.OTAK.INFO
Since 19 January 2007
Page hit : 885,289

Code update 24th June 2013
Brain is a very capable to solve big problems
but requires constant reminders about how to.
peace bird