首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

为啥不执行doget

2011-12-25 
为什么不执行dogetpackage com.michael.servletimport java.io.IOExceptionimport java.io.PrintWriter

为什么不执行doget
package com.michael.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import java.util.Enumeration;

import javax.servlet.*;
import javax.servlet.http.*;

public class ServletBasic extends HttpServlet {
  // private static final String CONTENT_TYPE = "text/html; charset=GBK";

  /* public void init(ServletConfig config) throws ServletException {
  super.init(config);
  }
*/
  public ServletBasic() {
  super();
  System.out.println("----ServletBasic-----");
  }

  public void destroy() {
  super.destroy();
  System.out.println("----destroy-----");
  }

  public void doGet(HttpServletRequest request, 
  HttpServletResponse response) throws ServletException, 
  IOException {
  // System.out.println("----doget-----");  
  String username = request.getParameter("username");
  String password = request.getParameter("password");
  System.out.println("username=" + username);
  if (username != null && username.equals("redking")) {
  request.getRequestDispatcher("/successful.html").forward(request, 
  response);
  } else {
  request.getRequestDispatcher("/failure.html").forward(request, 
  response);
  }


  /* System.out.println("----doget-----");
  response.setContentType("text/html");
  PrintWriter out = response.getWriter();
  out.println("<html>");
  out.println("<head><title>A Servlet</title></head>");
  out.println("<body>");
  out.println(" this is");
  out.println(this.getClass());
  out.println(",using the GET method");
  out.println("<p>The servlet has received a GET. This is the reply.</p>");
  out.println("<p>welcom to amaker!.</p>");
  out.println("</body></html>");
  out.close();*/
  }

  public void doPost(HttpServletRequest request, 
  HttpServletResponse response) throws ServletException, 
  IOException {
  System.out.println("----dopost-----");
  response.setContentType("text/html");
  PrintWriter out = response.getWriter();
  out.println("<html>");
  out.println("<head><title>A Servlet</title></head>");
  out.println("<body>");
  out.println(" this is");
  out.println(this.getClass());
  out.println(",using the POST method");
  out.println("<p>The servlet has received a POST. This is the reply.</p>");
  out.println("</body></html>");
  out.close();
  }

  public void init() throws ServletException {
  //第一种方法


  /*
  String driver = this.getServletContext().getInitParameter("driver");
  String url = this.getServletContext().getInitParameter("url");
  System.out.println(driver);
  System.out.println(url);
  System.out.println("----init-----");
  */
  //第二种方法
  /* Enumeration enu = this.getServletContext().getInitParameterNames();
  while(enu.hasMoreElements()){
  String name = (String)enu.nextElement();
  String value = this.getServletContext().getInitParameter(name);
  System.out.println(name+":"+value);
  }*/
  String username = this.getInitParameter("username");
  String password = this.getInitParameter("password");
  System.out.println(username);
  System.out.println(password);
  System.out.println("-----init-------");
  }
/*
  @override
  protected void service(HttpServletRequest arg0, 
  HttpServletResponse arg1) throws ServletException, 
  IOException {
  System.out.println("----service-----");
  }*/
}


[解决办法]
不执行doGet执行了doPost?
那你看看你表单提交的时候是不是将method=post

热点排行