我的jsp页面没有使用表单重定向到Servlet

时间:2014-04-22 16:46:20

标签: java jsp servlets

我的index.jsp页面的代码是

 <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    <h1>Welcome to the jsp page</h1>
    <form action="Myservlet" method="get" >
    <input type="button" value="send"/>
    </form>
    </body>
    </html>

这是我的servlet页面。

package mypackge.pkg;

import java.io.IOException;
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Myservlet
 */
public class Myservlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Myservlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        PrintWriter out = response.getWriter();
        out.println("I am into servlet");
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}

这是我的web.xml页面

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>WebDynamic</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>   
  </welcome-file-list>
  <servlet>
    <description>test servlet</description>
    <display-name>Myservlet</display-name>
    <servlet-name>Myservlet</servlet-name>
    <servlet-class>mypackge.pkg.Myservlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Myservlet</servlet-name>
    <url-pattern>/Myservlet</url-pattern>
  </servlet-mapping>
</web-app>

我的主页是.jsp页面,在点击发送按钮时,我需要转到servlet并显示一个简单的文本。但是当我点击发送按钮时,它不会重定向到显示servlet中的文本。 我正在使用tomcat 7和eclipse开普勒。 任何人都可以帮助我。 谢谢。

2 个答案:

答案 0 :(得分:1)

更改

<input type="button" value="send"/>

<input type="submit" value="send"/>

答案 1 :(得分:0)

你没有在servlet的doget方法中提到你没有得到的内容类型 &#34;我正在进入servlet&#34;

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws 
  ServletException, IOException
 {
    response.setContentType("text/html; charset=UTF-8");
    // TODO Auto-generated method stub
    PrintWriter out = response.getWriter();
    out.println("I am into servlet");
}