一个简单的Servlet注册实例

 

 我们在利用Servlet进行web开发的时候 ,运行的不是 JSP页面 而实际上是一个  Java类的对象  。这个类的对象从

javax.sevlet.http.HttpServlet ;这个类是基于HTTP的 Servlet类 我们自己写的 Servlet应该从 这个类派生而来 .

通过我们自己编写的类 以及对web.xml的部署  ,进行web的流程控制 ,实现用户与服务器的交互 。

要注意在 编写Servlet进行相应客户端的时候 我们在调用  Response.setContenType("text/html;charset=gb2312");的时候一定要放在其他函数调用的前面 因为这是设置 相应给客户端页面的 MIME类型 ,设置真个页面的属性。  这个和JSP页面的  <%@ page contenType="text.html;charset=gb2312"%> 意义是一样的 ,如果我们不设置

那么 页面中的交互文本将是乱码 。。。。

下面是一个简单的 Servlet注册代码

 

运行 Tomcat的时候输入如下提交

http://localhost:8888/firstServlet?user=123&password=123     //进行 Servlet提交

web工程的web.xml编写如下代码

<?xml version="1.0" encoding="GB2312"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet> 
    <servlet-name>firstServlet</servlet-name>    //实例名字
    <servlet-class>me.RServlet</servlet-class>  //类
</servlet>
<servlet-mapping>
    <servlet-name>firstServlet</servlet-name>    //实例名字
    <url-pattern>/firstServlet</url-pattern>   //URL访问的时候相对于 WEB应用的 逻辑路径
</servlet-mapping>
 <display-name>
 ch08</display-name>
 <welcome-file-list>       //这是一个默认主页的配置
  <welcome-file>index.html</welcome-file>
  <welcome-file>index.htm</welcome-file>
  <welcome-file>index.jsp</welcome-file>
  <welcome-file>default.html</welcome-file>
  <welcome-file>default.htm</welcome-file>
  <welcome-file>default.jsp</welcome-file>
 </welcome-file-list>
</web-app>

 

 

Servlet代码 ,将生成的  RServlet.class复制到 WEB-INF下的 classes目录下      

package me;
import javax.servlet.*; 
import javax.servlet.http.*;  //基于http的 HttpServlet类在此包中
import java.io.*;
import java.sql.*;
public class RServlet extends HttpServlet     //从HttpServlet类派生而来 至少实现以下一个函数
{
  String user  ;
  String password;
  PrintWriter out ;//输出流
  public void init(ServletConfig config) throws ServletException
  {
   super.init(config) ;//调用基类的构造函数进行初始化
  }
  public  void service(ServletRequest req, ServletResponse res) //重写Service来对客户端的请求进行相应
  {  
   res.setContentType("text/html;charset=GB2312");//设置相应页面的格式 如果不设置那么将会导致出现乱码
   //设置 MIME类型应该在 获取PrintWriter之前
   this.getPrintWriter(res);//获得输出流
   if((user=req.getParameter("user"))==null||(password=req.getParameter("password"))==null)//数据不合理就返回
   {    
        out.print("非法数据提交!<br>");
        return ;
   }
   else
   {
     this.addUserToDB(this.user, this.password)   ;//增加用户到数据库
   }
  }
public  void  getPrintWriter(ServletResponse res)
{
 try
 {
   this.out=res.getWriter() ;
 }
 catch(Exception e)
 {
  
 }
}
public  void addUserToDB(String user,String pass)//增加用户数据到数据库
{   
  try
  {
  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ;
   Connection cn=DriverManager.getConnection("jdbc:odbc:testDatabase","sa","7603835");//构造一个Connection对象表示和一个数据库的链接
   Statement s=cn.createStatement() ;//创建Statement对象
   String sql="insert into info(username,password) values('"+user+"','"+pass+"')";
   s.executeUpdate(sql) ;//执行SQL语句
   cn.close() ;
  }
  catch(Exception e)
  {
   out.print("Error");
  }
  out.print("数据添加成功!");
}

}
 

posted on 2011-12-15 21:51  风尘女子  阅读(301)  评论(0编辑  收藏  举报

导航