package com.login.controller;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;
import javax.servlet.http.HttpSession;public class ExitServlet extends HttpServlet { /**
 * The doPost method of the servlet. <br>
 *
 * This method is called when a form has its tag value method equals to post.
 * 
 * @param request the request send by the client to the server
 * @param response the response send by the server to the client
 * @throws ServletException if an error occurred
 * @throws IOException if an error occurred
 */
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { HttpSession session=request.getSession();
session.invalidate();
response.sendRedirect("Login.jsp");
}

}HTTP method GET is not supported by this URL
ExitServlet回传login.jsp时出了错误 为什么只能使用doGet()方法

解决方案 »

  1.   

    没试过!可能就是不能使用post去重定向吧!
      

  2.   

    doPost里需要调用一下doGet方法的。。
      

  3.   

    用谁都行, 默认调用的是doGet ,如果你要想写在doPost 里,你就在doGet 里面调用doPost
      

  4.   


    什么意思  service方法不是servlet类的吗。。 现在是重写HttpServlet本人菜鸟 求指点
      

  5.   

    补充点 有这么几个文件login.jsp<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <base href="<%=basePath%>"></head><body>
    <%=request.getAttribute("Loginerror")==null?"":request.getAttribute("Loginerror") %>
    <form action="LoginServlet" method="post">
    username:<input type="text" name="username"><br>
    upassword:<input type="password" name="upassword"><br> 
    <input type="submit" value="submit"><br>
    </form>
    </body>
    </html>LoginServlet
    package com.login.controller;import java.io.IOException;
    import java.io.PrintWriter;import javax.jms.Session;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    import javax.servlet.jsp.PageContext;import com.login.model.*;
    public class LoginServlet extends HttpServlet { /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    String username=request.getParameter("username");
    String password=request.getParameter("upassword");

    if(username!=null&&password!=null){
    if(username.equals("a")&&password.equals("123")){
    User user=new User(username, password);
    HttpSession session=request.getSession(true);
    session.setAttribute("userinfo", user);
    request.getRequestDispatcher("LoginOK.jsp").forward(request, response);
    return;
    }
    else{
    request.setAttribute("Loginerror", "loginerror");
    request.getRequestDispatcher("Login.jsp").forward(request, response); }
    }else{
    response.sendRedirect("Login.jsp");

    } }}LoginOK.jsp<%@page import="com.login.model.User"%>
    <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
         </head>
      
      <body>
      hello&nbsp;<%=((User)session.getAttribute("userinfo")).getNameString() %>
        <br>
        <a href="ExitServlet">exit</a>
      </body>
    </html>
    LoginServlet.javapackage com.login.controller;import java.io.IOException;
    import java.io.PrintWriter;import javax.jms.Session;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    import javax.servlet.jsp.PageContext;import com.login.model.*;
    public class LoginServlet extends HttpServlet { /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    String username=request.getParameter("username");
    String password=request.getParameter("upassword");

    if(username!=null&&password!=null){
    if(username.equals("a")&&password.equals("123")){
    User user=new User(username, password);
    HttpSession session=request.getSession(true);
    session.setAttribute("userinfo", user);
    request.getRequestDispatcher("LoginOK.jsp").forward(request, response);
    return;
    }
    else{
    request.setAttribute("Loginerror", "loginerror");
    request.getRequestDispatcher("Login.jsp").forward(request, response); }
    }else{
    response.sendRedirect("Login.jsp");

    } }}
    ExitServlet.javapackage com.login.controller;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;
    import javax.servlet.http.HttpSession;public class ExitServlet extends HttpServlet { /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException { HttpSession session=request.getSession();
    session.invalidate();
    response.sendRedirect("Login.jsp");
    }

    }问题
    只要ExitServlet改成doGet才能不报错  如果是doPost()就会出现
    HTTP method GET is not supported by this URL求指点
    另:jsp和servlet的标准api在哪看
      

  6.   

    谢谢各位 弄清楚了 应该是<a> 标签自动调用HttpServlet的doGet()方法
      

  7.   

    a标签发送的url字符串链接,就是get请求。。
      

  8.   

    楼上说的对,
    a标签发送的url字符串链接,就是get请求。。
    如果楼主实在想用doPost方法,
    就在doGet方法中加一句
    this.doPost(request, response);
    就可以了。
      

  9.   

    我遇到过,重定向里要用全路径,跟路径用basePath、getContentPath方法获得