创建servlet的3种方法实例。1.调用servlet接口 在webapps下创建3个文件夹, classes存放servlet类文件。web.xml为这个web应用的配置文件。 类文件:
package com.ruweb;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.Date;
public class myServlet implements Servlet{
//初始化函数,及将这个servlet装载到内存中。这个恶函数只会被调用一次
public void init(ServletConfig config)throws ServletException{
}
//得到servletconfig对象
public ServletConfig getServletConfig(){
return null;
}
//服务函数,这个函数用于实现逻辑过程,会被多次调用。
public void service(ServletRequest req,ServletResponse res)throws ServletException,java.io.IOException{
System.out.println(“1111″);
res.getWriter().println(“hello,world!”+new Date().toLocaleString());
}
//得到servlet的配置信息
public java.lang.String getServletInfo(){
return null;
}
//销毁内存,释放内存,只调用一次。
public void destroy(){
}
}
web.xml Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the “License”); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an “AS IS” BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd”
version=”2.5″>
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
<servlet>
<servlet-name>myServlet</servlet-name>
<servlet-class>com.ruweb.myServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myServlet</servlet-name>
<url-pattern>/servlet</url-pattern>
</servlet-mapping>
web.xml
<?xml version=”1.0″ encoding=”ISO-8859-1″?>
<!–
–>
<web-app xmlns=”http://java.sun.com/xml/ns/javaee”
</web-app>
2.servlet的生命周期
3.第二种继承GenericServlet
类文件中只有service()函数,其他函数都封装到了GenericServlet()中, ServletResponse res)
throws ServletException,
java.io.IOException{
HttpServletResponse resp)
throws ServletException,
java.io.IOException{
HttpServletResponse resp)
throws ServletException,
java.io.IOException{
resp.setCharacterEncoding(“utf-8″);
}
类文件中只有service()函数,其他函数都封装到了GenericServlet()中,
package com.MyGenericServlet;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class MyGenericServlet extends GenericServlet{
public void service(ServletRequest req,
res.setCharacterEncoding(“utf-8″);
res.getWriter().println(“rushen”);
}
}
4.最重要的servlet开发方法,HttpServlet
package com.MyHttpServlet;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.Date;
public class MyHttpServlet extends HttpServlet{
protected void doGet(HttpServletRequest req,
resp.setCharacterEncoding(“utf-8″);
resp.getWriter().println(“如也”+new Date().toLocaleString());
}
protected void doPost(HttpServletRequest req,
resp.getWriter().println(“如神”+new Date().toLocaleString());
}
在HttpServlet中两种接收传递对象的方式doGet()和doPost(),默认使用doGet();
二.
1.用网页调用servlet 在web应用下(web应用的名字webapps/ru2/)新建index.html
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>MyHttpServlet</title>
</head>
<body>
<form action=”/ru2/myhs” method=”post”>
<input type=”text” name=”input1″/>
<input type=”submit” value=”submit”/>
</form>
</body>