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

来源:1-3 手工编写第一个Servlet

干巴爹forever

2017-10-31 20:34

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

写回答 关注

2回答

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

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



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

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

      </form>

    干巴爹for...

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

    2017-11-01 19:54:41

    共 2 条回复 >

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

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

    慕妹7264... 回复干巴爹for...

    那是Tomcat启动成功的信息, 不是错误,放心吧

    2017-11-01 21:17:27

    共 3 条回复 >

JAVA遇见HTML——Servlet篇

本门课程在JSP课程的基础上,深入介绍Servlet的基础知识

160615 学习 · 1029 问题

查看课程

相似问题