繁体   English   中英

将JSP映射到servlet

[英]Map JSP to servlet

尝试将jsp提交到servlet。 web.xml映射期间发生错误。

我有

的index.jsp

<form method="POST" action="Validate">
     <input type="submit" value="Submit" />
</form>

web.xml中

<servlet>
    <servlet-name>validate</servlet-name>
    <servlet-class>com.test.Validate</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>validate</servlet-name>
    <url-pattern>/Validate</url-pattern>
  </servlet-mapping>

文件夹结构

在此处输入图片说明

但是,当我尝试在服务器上为index.jsp运行时,出现"server cannot be started"错误

从web.xml中删除servlet映射时出错

Validate.java

package com.test;

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

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Validate
 */
@WebServlet(description="validation", urlPatterns={"/Validate"})
public class Validate extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Validate() {
        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();
        response.setContentType("text/html");
        System.out.println("validate called");
    }

当我在Validate.java上作为服务器运行时,它可以工作

但是,当我在index.jsp上作为服务器运行并单击提交时。 它重定向到http://localhost:8080/TestApp/Validate ,控制台上没有任何内容。

问题是您同时使用了注释映射和web.xml因此最好删除其中之一以使您的代码正常工作。我相信注释之一会更好。

@WebServlet(description="validation", urlPatterns={"/Validate"})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM