日期:2014-05-17  浏览次数:20833 次

java中如何创建Servlet
如题 求高手指教

------解决方案--------------------
Java code
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class TestServlet extends HttpServlet {

    private static final long serialVersionUID = 8009071316884480626L;


    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
        out.println("<HTML>");
        out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
        out.println("  <BODY>");
        out.print("    This is ");
        out.print(this.getClass());
        out.println(", using the GET method");
        out.println("  </BODY>");
        out.println("</HTML>");
        out.flush();
        out.close();
    }

    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        this.doGet(request, response);
    }

    
}

------解决方案--------------------
创建一个class extends HttpServlet {
}

就是创建一个servlet了 

Java code
java jsp+servlet实现登录网页设计
  
这次涉及到四个文件:
登录页面:login.jsp
登录成功欢迎页面:success.jsp
登录失败页面:fail.jsp
Servlet处理文件:LoginServlet.java
其实还涉及到一个文件:web.xml,这个后面再说:
下面分别介绍这几个文件:
//--------------------------------------------
LoginServlet.java   内容:
package org.cai.servlet ;
import java.sql.* ;
import java.io.* ;
import javax.servlet.http.* ;
import javax.servlet.* ;
public class LoginServlet extends HttpServlet implements Servlet{
 public LoginServlet(){
 }
 
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
 
 }
// protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
//  System.out.println("-------------------------") ;
//  doPost(request, response) ;
// }
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
  response.setContentType("text/html;charset=gb2312") ;
  request.setCharacterEncoding("gb2312") ;
  String result = "" ;
  //获取用户名
  String username = request.getParameter("username") ;
  String psw = request.getParameter("password") ;
  if ((username == "") || (username==null) || (username.length() > 20 )){
   try{
    result = "请输入用户名(不能超过20个字符)!" ;
    request.setAttribute("message" ,result) ;
    response.sendRedirect("login.jsp") ;
   }catch(Exception e){
    e.printStackTrace() ;
   }
  }
  if ((psw == "") || (psw==null) || (psw.length() > 20 )){
   try{
    result = "请输入密码(不能超过20个字符)!" ;
    request.setAttribute("message" ,result) ;
    response.sendRedirect("login.jsp") ;
   }catch(Exception e){
    e.printStackTrace() ;
   }
  }
  
  //登记JDBC驱动程序
  try{
   Class.forName("org.gjt.mm.mysql.Driver") ;
  }catch(
                         ognl获取user.roles[零].roleId报错!