QUSSERS1991 2014-12-22 09:18 采纳率: 0%
浏览 1681

intellij idea 13中web工程写了一个servlet

intellij idea 13中web工程写了一个servlet (里面写了一个输出语句)
启动服务器,为什么ie页面显示的是index.jsp内容,而不是servlet内容。
servlet写在YY.CLASS这个类里面:

import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;

/**

  • Created by ZHIYU on 2014/12/22. */ public class YY extends GenericServlet { @Override public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { servletResponse.getOutputStream().write("wwwwwwwwwwwwwwwwww".getBytes()); } }

web.xml做了配置:

YY
YY


YY
/YY

    不解为什么会这样 新手刚开始学 求热心人解答下
  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-08-03 04:47
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    你已经正确地定义了你的GenericServlet。现在你需要创建一个ServletConfig对象来获取相关的请求和响应信息。这是如何做到的:

    public class MyServlet extends GenericServlet {
    
        public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
            // Do something with the request and response
        }
    }
    

    然后你可以使用getServletContext()方法来获取到应用的上下文(application context),并使用它来获取到请求和响应的信息。这将帮助你理解问题的原因。

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // Get the application context for this servlet
        ApplicationContext context = getServletContext();
    
        // Get the request object
        Request requestObj = (Request) context.getAttribute(Request.class.getName());
    
        // Get the response object
        Response responseObj = (Response) context.getAttribute(Response.class.getName());
    }
    

    请注意,这些代码片段是基于JSP 2.4的,如果你正在使用的是更新的版本,请确保按照相应的API文档进行调整。

    评论

报告相同问题?