标签: servlets

java.lang.IllegalArgumentException:servlet映射中的<url-pattern>无效

<servlet>
    <servlet-name>myservlet</servlet-name>
    <servlet-class>workflow.WDispatcher</servlet-class>
    <load-on-startup>2</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>myservlet</servlet-name>
    <url-pattern>*NEXTEVENT*</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)

以上是Tomcat的片段web.xml.*NEXTEVENT*启动时的URL模式抛出

java.lang.IllegalArgumentException:servlet映射中的<url-pattern>无效

如果有人能提示错误,将不胜感激.

tomcat web.xml servlets illegalargumentexception servlet-mapping

40
推荐指数
1
解决办法
8万
查看次数

如何正确地将HTTP消息发送到客户端

我正在使用Java的RESTful Web服务.如果出现问题,我需要一种向客户端发送错误消息的好方法.

根据Javadoc,由于消息参数含义模糊,HttpServletResponse.setStatus(int status, String message)因此不推荐使用.

是否有一种首选方式来设置响应的状态消息或" 原因短语 "?该sendError(int, String)方法没有这样做.

编辑:为了澄清,我想修改HTTP状态行,即"HTTP/1.1 404 Not Found"不是正文内容.具体来说,我想发送回复"HTTP/1.1 400 Missing customerNumber parameter".

java servlets http status java-ee

40
推荐指数
3
解决办法
6万
查看次数

在JSP/Servlet中填充级联下拉列表

假设我有三个名为的下拉列表控件dd1,dd2dd3.每个下拉列表的值来自数据库.dd3价值取决于价值dd2dd2价值取决于价值dd1.任何人都可以告诉我如何调用servlet来解决这个问题?

jsp servlets cascading drop-down-menu

40
推荐指数
1
解决办法
6万
查看次数

如何在servlet过滤器中获取Spring bean?

我已经定义了一个javax.servlet.Filter带有Spring注释的Java类.

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Bean;

@Configuration
public class SocialConfig {

    // ...

    @Bean
    public UsersConnectionRepository usersConnectionRepository() {
        // ...
    }
}
Run Code Online (Sandbox Code Playgroud)

我想把豆子放进UsersConnectionRepositoryFilter,所以我尝试了以下方法:

public void init(FilterConfig filterConfig) throws ServletException {
    UsersConnectionRepository bean = (UsersConnectionRepository) filterConfig.getServletContext().getAttribute("#{connectionFactoryLocator}");
}
Run Code Online (Sandbox Code Playgroud)

但它总是回归null.我怎样才能获得一个Spring bean Filter

java spring servlets servlet-filters

40
推荐指数
3
解决办法
4万
查看次数

如何用Maven属性替换web.xml中的值?

我有一个Maven项目,它将一些测试文件下载到它的构建目录中./target/files.然后,这些文件应该可用于servlet,我可以通过将完整路径硬编码为servlet来轻松实现<init-param>:

<servlet>
    <servlet-name>TestServlet</servlet-name>
    <servlet-class>my.package.TestServlet</servlet-class>
    <init-param>
        <param-name>filepath</param-name>
        <param-value>/home/user/testproject/target/files</param-value>
    </init-param>
</servlet>
Run Code Online (Sandbox Code Playgroud)

如何避免硬编码完整路径并使用动态参数替换?我尝试了以下,但它不起作用:

<param-value>${project.build.directory}/files</param-value>
Run Code Online (Sandbox Code Playgroud)

parameters web.xml servlets maven

40
推荐指数
2
解决办法
5万
查看次数

在Web应用程序中处理上下文的任何聪明方法?

在Java中,Web应用程序捆绑在WAR中.默认情况下,许多servlet容器将使用WAR名称作为应用程序的上下文名称.

因此myapp.war被部署到http://example.com/myapp.

问题是webapp认为它的"root"是"root",或者只是"/",而HTML会认为应用程序的根目录是"/ myapp".

Servlet API和JSP具有帮助管理它的工具.例如,如果在servlet中执行:response.sendRedirect("/ mypage.jsp"),则容器将在上下文之前创建url:http://example.com/myapp/mypage.jsp ".

但是,您不能使用HTML中的IMG标记来执行此操作.如果你做<img src ="/ myimage.gif"/>你可能会得到404,因为你真正想要的是"/myapp/myimage.gif".

许多框架都具有可识别上下文的JSP标记,并且在JSP中有不同的方法来制作正确的URL(没有特别优雅的方法).

对于编码人员来说,跳出何时使用"App Relative"网址与绝对网址是一个很小的问题.

最后,还存在需要动态创建URL的Javascript代码以及CSS中的嵌入式URL(用于背景图像等)的问题.

我很好奇其他人使用什么技术来缓解和解决这个问题.许多人只是简单地将其编写并硬编码,无论是服务器根目录还是他们碰巧使用的任何上下文.我已经知道答案了,那不是我想要的.

你是做什么?

java jsp servlets contextpath

39
推荐指数
2
解决办法
3万
查看次数

使用ServletOutputStream在Java servlet中编写非常大的文件而不会出现内存问题

我正在使用IBM Websphere Application Server v6和Java 1.4,并且正在尝试将大型CSV文件写入ServletOutputStream供用户下载.目前文件范围为50-750MB.

较小的文件不会导致太多问题,但是对于较大的文件,它似乎被写入堆中,然后导致OutOfMemory错误并关闭整个服务器.

这些文件只能通过HTTPS提供给经过身份验证的用户,这就是我通过Servlet服务它们而不是仅仅将它们粘贴在Apache中的原因.

我正在使用的代码是(在此周围删除了一些绒毛):

    resp.setHeader("Content-length", "" + fileLength);
    resp.setContentType("application/vnd.ms-excel");
    resp.setHeader("Content-Disposition","attachment; filename=\"export.csv\"");

    FileInputStream inputStream = null;

    try
    {
        inputStream = new FileInputStream(path);
        byte[] buffer = new byte[1024];
        int bytesRead = 0;

        do
        {
            bytesRead = inputStream.read(buffer, offset, buffer.length);
            resp.getOutputStream().write(buffer, 0, bytesRead);
        }
        while (bytesRead == buffer.length);

        resp.getOutputStream().flush();
    }
    finally
    {
        if(inputStream != null)
            inputStream.close();
    }
Run Code Online (Sandbox Code Playgroud)

FileInputStream似乎没有,如果我写到另一个文件或只是删除完全写入内存使用情况似乎并不成为一个问题而导致问题.

我的想法是resp.getOutputStream().write存储在内存中,直到数据可以发送到客户端.因此整个文件可能会被读取并存储在resp.getOutputStream()导致我的内存问题和崩溃的位置!

我已经尝试过缓冲这些流,并尝试使用Channels java.nio,其中任何一个似乎都没有对我的内存问题产生任何影响.我还在OutputStream每次迭代循环和循环之后刷新了一次,这没有帮助.

java websphere servlets stream

39
推荐指数
1
解决办法
9万
查看次数

获取错误:元素类型"web-app"的内容必须匹配,

当我在Eclipse Helios Service Release 2中构建我的项目时,我的错误web.xml.请建议我为此做些什么.在我的项目中,我使用的是DTD 2.2.错误如下.

元素类型"web-app"的内容必须匹配"(icon?,display-name ?, description?,distributable?,context-param*,servlet*,servlet-mapping*,session-config?,mime- mapping*,welcome-file-list?,error-page*,taglib*,resource-ref*,security-constraint*,login-config?,security-role*,env-entry*,ejb-ref*)".

web.xml servlets

39
推荐指数
1
解决办法
3万
查看次数

ServletContainerInitializer与ServletContextListener

我试图注册使用servletContainerInitializer一个servlet,但它似乎不工作,也许这是我的代码(好心审查),但是我到想知道的区别ServletContainerInitializerServletContextListener,因为使用时follwing代码运行正常ServletContextListener,而不是.

从servlet 3.0规范:

4.4

配置方法(动态添加servlet):

......或者从实施onStartup方法ServletContainerInitializer......

ServletContainerInitializer:

package com.marmoush.javaexamples.nullhaus.servlet;

import java.util.Set;
import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

public class MyInit implements ServletContainerInitializer {
    public void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletException {
        System.out.println("hello");
        ServletRegistration reg = ctx.addServlet("q31","com.marmoush.javaexamples.nullhaus.servlet.Q31");
        reg.addMapping("/q31/*");
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试自动注册的servlet:

package com.marmoush.javaexamples.nullhaus.servlet;

import java.io.IOException;

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

public class Q31 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) …
Run Code Online (Sandbox Code Playgroud)

servlets initialization

39
推荐指数
2
解决办法
2万
查看次数

web.xml中支持异步的目的是什么?

<servlet>
        <description>xxx</description>
        <servlet-name>xxx</servlet-name>
        <servlet-class>com.xxx.yyy</servlet-class>
        <async-supported>true</async-supported>
</servlet>
Run Code Online (Sandbox Code Playgroud)

async-supportedservlet的web.xml配置文件的目的是什么?我可以在什么情况下使用它?

java asynchronous web.xml servlets

38
推荐指数
1
解决办法
3万
查看次数