问答详情
源自:1-3 手工编写第一个Servlet

get方法可以。post方法不可以

index。jsp代码如下:

<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <base href="<%=basePath%>">

    

    <title>My JSP 'index.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">    

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

<link rel="stylesheet" type="text/css" href="styles.css">

-->

  </head>

  

  <body>

    <h1>第一个servlet小例子</h1>

    <a href="servlet/HelloServlet">get请求servlet</a><br>

    

     <form action="servlet/HelloServlet"  method="post">

    <input type="submit" value="post请求servlet"/>

  

  </body>

</html>

HelloServlet.java代码如下:

package servlet;


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 HelloServlet extends HttpServlet {


@Override

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

// TODO Auto-generated method stub

System.out.println("处理get()请求。。。。");

PrintWriter out=response.getWriter();

response.setContentType("text/html;charset=utf-8");

out.println("hello servlet!");

}


@Override

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

// TODO Auto-generated method stub

System.out.println("处理post()请求。。。。");

PrintWriter out=response.getWriter();

response.setContentType("text/html;charset=utf-8");

out.println("Hello servlet!");

}


}

//img3.mukewang.com/59f86da50001ea8f08560567.jpg

提问者:干巴爹forever 2017-10-31 20:34

个回答

  • 慕妹7264492
    2017-10-31 23:22:39
    已采纳

    你的form结束标签呢? </form>



       <form action="servlet/HelloServlet"  method="post">

        <input type="submit" value="post请求servlet"/>

      </form>

  • 干巴爹forever
    2017-11-01 19:54:02

    加了</form>以后post确实可以用了,但是console面和老师的还是不一样,我的还是红红的一片。。