相关疑难解决方法(0)

servlet如何工作?实例化,会话,共享变量和多线程

假设,我有一个拥有大量servlet的Web服务器.对于在这些servlet之间传递的信息,我正在设置会话和实例变量.

现在,如果有2个或更多用户向此服务器发送请求,那么会话变量会发生什么?它们对所有用户都是通用的,或者对于每个用户而言都是不同的.如果它们不同,那么服务器如何区分不同的用户?

还有一个类似的问题,如果有n用户访问特定的servlet,那么这个servlet只在第一个用户第一次访问它时实例化,或者是否为所有用户单独实例化?换句话说,实例变量会发生什么?

java multithreading servlets session-variables instance-variables

1105
推荐指数
6
解决办法
28万
查看次数

如何在Spring MVC中处理静态内容?

我正在使用Spring MVC 3开发一个webapp,并且DispatcherServlet像'so'一样捕获所有请求(web.xml):

  <servlet>
    <servlet-name>app</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>

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

现在这可以像宣传的那样工作,但是我如何处理静态内容呢?以前,在使用RESTful URL之前,我会抓住所有*.html并将其发送给DispatcherServlet,但现在它是一个不同的球类游戏.

我有一个/ static /文件夹,其中包含/ styles /,/ js /,/ images/etc,我想从中排除/ static/*DispatcherServlet.

现在,当我这样做时,我可以获得静态资源:

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

但我希望它有很好的URL(我使用Spring MVC 3)而不是登陆页面www.domain.com/app/

我也不希望解决方案耦合到tomcat或任何其他servlet容器,并且因为这是(相对)低流量我不需要网络服务器(如apache httpd)infront.

有一个干净的解决方案吗?

spring-mvc

197
推荐指数
9
解决办法
21万
查看次数

Servlet返回"HTTP状态404请求的资源(/ servlet)不可用"

我的文件WebContent/jsps夹中的JSP文件中有一个HTML表单.我servlet.javasrc文件夹中的默认包中有一个servlet类.在我web.xml的映射为/servlet.

action在HTML表单的属性中尝试了几个URL :

<form action="/servlet">
Run Code Online (Sandbox Code Playgroud)
<form action="/servlet.java">
Run Code Online (Sandbox Code Playgroud)
<form action="/src/servlet.java">
Run Code Online (Sandbox Code Playgroud)
<form action="../servlet.java">
Run Code Online (Sandbox Code Playgroud)

但这些都不起作用.他们都在Tomcat 6/7/8中继续返回如下所示的HTTP 404错误:

HTTP状态404 - /servlet

描述:请求的资源(/ servlet)不可用.

或者如下面的Tomcat 8.5/9:

HTTP状态404 - 未找到

消息:/ servlet

描述:源服务器没有找到目标资源的当前表示,或者不愿意透露存在该资源

为什么不起作用?

html forms jsp servlets http-status-code-404

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

在/*上映射全局前端控制器servlet时如何访问静态资源

我将Spring MVC调度程序映射为全局前端控制器servlet /*.

<servlet>       
  <servlet-name>home</servlet-name>         
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>     
</servlet>  
<servlet-mapping>       
  <servlet-name>home</servlet-name>         
  <url-pattern>/*</url-pattern>     
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)

但是,此映射会停止访问CSS,JS,图像等静态文件,这些/res/文件都在文件夹中.

我怎么能访问它们?

java resources servlets

58
推荐指数
8
解决办法
11万
查看次数

如何在web.xml中配置欢迎文件列表

我在我的web.xml文档中有这个.我想要一个欢迎列表,所以我不需要再输入主页的路径了.但每次单击我的tomcat页面中的应用程序时,它都会显示所请求的资源不可用.

<listener>
    <listener-class>web.Init</listener-class>
</listener>

<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<servlet>
    <servlet-name>index</servlet-name>
    <servlet-class>web.IndexServlet</servlet-class>
</servlet>

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

我的jsp页面的servlet

package web;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

public class IndexServlet extends HttpServlet
{
    private Logger logger = Logger.getLogger(this.getClass());
    private RequestDispatcher jsp;

    public void init(ServletConfig config) throws ServletException
    {
        ServletContext context = config.getServletContext();
        jsp = context.getRequestDispatcher("/WEB-INF/jsp/index.jsp");
    }

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        logger.debug("doGet()");
        jsp.forward(req, resp); 
    }
} …
Run Code Online (Sandbox Code Playgroud)

jsp web.xml servlets welcome-file

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

如何防止映射在/*上的前端控制器servlet处理静态资源

我有一个servlet作为前端控制器.

@WebServlet("/*")
Run Code Online (Sandbox Code Playgroud)

但是,这也处理CSS和图像文件.我怎么能阻止这个?

servlets

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

Web框架应该是Filter还是Servlet?

让Web框架处理来自单个入口点的请求是一个已解决的问题.但是,单个入口点应该是Filter还是Servlet?为什么Web应用程序开发人员更喜欢一个?为什么框架开发人员更喜欢一个?

java servlets java-ee servlet-filters

11
推荐指数
1
解决办法
2016
查看次数

使用doGet将根路径中的默认主页更改为servlet

我有一个小maven(间接通过Netbeans 8.1和tomcat设置)

每当我运行项目时,它会在根目录上打开带有HelloWord的浏览器:

即页面http://localhost:8084/是:

<html>
    <head>
        <title>Start Page</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <h1>Hello World!</h1>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

我试图创建一个servlet来替换它:

@WebServlet(name = "HomeServlet", urlPatterns = {"/"}) 但是,它没有按预期工作.

即它仍然表现出同样的问候世界: http://localhost:8084

但它确实搞乱了根目录上的所有文件,即http://localhost:8084/foo.css由这个servlet处理并获得响应.

所以,我的问题是(实际上是两个):

如何将此页面的内容更改为其他内容?

或者,至少(如果前者不可能):我可以在根路径上使用永久重定向来避免用户看到此页面吗?

(即http代码301)将用户移动到 http://localhost:8084/home

servlets homescreen

10
推荐指数
1
解决办法
5613
查看次数

如何调试在spring mvc rest中找不到的404资源?

我有一个示例spring rest mvc应用程序,它具有以下java代码:

SampleController.java

import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("sample")
public class SampleController {
        @RequestMapping(method = RequestMethod.GET, produces = "application/json")
        @ResponseBody
        public String getBatches()//@RequestParam(name = "name", required = true) String name)
        {
                return "Hello ";
        }
}
Run Code Online (Sandbox Code Playgroud)

的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>ved</groupId>
    <artifactId>platform</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>platform Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <properties>
        <spring.version>4.2.1.RELEASE</spring.version>
        <jackson.version>2.6.2</jackson.version>
        <spring-boot.version>1.2.6.RELEASE</spring-boot.version>
        <filter.name>DEV</filter.name>
        <jersey.version>1.9</jersey.version>
        <base.directory>${basedir}</base.directory>
    </properties>
    <profiles>
        <profile>
            <id>local</id>
            <activation> …
Run Code Online (Sandbox Code Playgroud)

java maven spring-restcontroller spring-rest

9
推荐指数
2
解决办法
9125
查看次数

在单个web.xml中添加多个servlet

我试图在一个web.xml中运行两个Servlet类,但它不起作用,每个servlet类独立工作正常.

web.xml:

<servlet>
    <servlet-name>spring-ws</servlet-name>
    <servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>
    <init-param>
        <param-name>transformWsdlLocations</param-name>
        <param-value>true</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>spring-ws</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

<session-config>
    <session-timeout>240</session-timeout>
</session-config>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring-ws-servlet.xml
     /WEB-INF/health-page-servlet.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
    <servlet-name>health-page</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>health-page</servlet-name>
    <url-pattern>/health.htm</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)

如果你能解决我正在做的事情,请告诉我.

我尝试了以下链接,但它对我不起作用我 可以在一个应用程序中使用Spring MVC和Spring WS吗?

java spring servlets

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