我有一个部署在http:// ip:8080/simple下
的servlet servlet在包下a.b.c
我有一个a.b.resources命名的html页面Test.html.
html有img一个图像标签.
在我做的servlet中:
htmlFile = MyServlet.class.getResourceAsStream("/a/b/resources/Test.html");
resp.setContentType("text/html");
PrintWriter writer = resp.getWriter();
byte[] bytes=new byte[htmlFile.available()];
htmlFile.read(bytes);
resp.setContentLength(bytes.length);
writer.print(new String(bytes));
writer.flush();
writer.close();
Run Code Online (Sandbox Code Playgroud)
html页面出现在浏览器上,但在图像的位置我看到它的alt描述.
我试过了:
<img alt="Company A" src="./CompanyLogo.jpg">
<img alt="Company A" src="/a/b/resources/CompanyLogo.jpg">
<img alt="Company A" src="CompanyLogo.jpg">
Run Code Online (Sandbox Code Playgroud)
但这些都不起作用.
jpg图像位于/ a/b/c/resources下,即与HTML页面位于同一目录中.
我正在使用嵌入式Jetty.
我在这里乱搞什么?
我正在尝试配置嵌入式Jetty(7.6)来同时处理WebSocketHandler,ServletContextHandler和ResourceHandler类.
我已经尝试过使用HandlerCollection和HandlerList类,但我不能让所有3个部分都能工作.
Server server = new Server(8081);
// static files handler
ResourceHandler resourceHandler = new ResourceHandler();
resourceHandler.setDirectoriesListed(true);
resourceHandler.setResourceBase("./src/main/webapp/");
// servlet handler
ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
servletContextHandler.setContextPath("/");
servletContextHandler.addServlet(new ServletHolder(new myServlet()), "/myServlet");
// websocket handler
myWebSocketHandler myWebSocketHandler = new myWebSocketHandler();
// putting it together
HandlerCollection handlerList = new HandlerCollection();
handlerList.setHandlers(new Handler[]{resourceHandler,myWebSocketHandler,servletContextHandler});
server.setHandler(handlerList);
Run Code Online (Sandbox Code Playgroud)
在这种状态下,静态文件和servlet处理得很好,但是当我发送websocket请求时,我得到:
2012-02-22 10:16:44.703:WARN:oejs.Response:Committed before 503 null
2012-02-22 10:16:44.705:WARN:oejs.AbstractHttpConnection:/
java.lang.IllegalStateException: Committed
at org.eclipse.jetty.server.Response.resetBuffer(Response.java:1080)
...
Run Code Online (Sandbox Code Playgroud)
如果我在我的websockets处理程序中检查基本请求"处理"属性的状态,它已经设置为true.所以这意味着我的请求正在处理并提交到myWebSocketHandler之前提交?
任何评论都表示赞赏,谢谢.
有没有办法向正在运行的嵌入式Jetty实例添加处理程序?我们已将旧的基于Jetty 6的项目迁移到Jetty 9,我们需要为插件系统添加和删除动态处理程序......
见下面的例子......
Server server = new Server();
[...]
server.start();
[...]
Handler[] existingHandler = server.getHandlers();
// There is no more
server.addHandler(newHandler);
// only this you can do, but only if the server is stopped
server.setHandler(newHandler)
Run Code Online (Sandbox Code Playgroud)
注意:newHandler是HandlerCollection......
我想使用jetty启动我的应用程序,所以我添加了下面提到的依赖项.当我运行主方法Jetty成功启动.(我正在开发一个struts2 + spring3 + hibernate maven项目,我也可以在tomcat中部署它)
现在我想从战争包装pom创建一个可执行jar.所以我在我的pom中添加了maven-assembly-plugin.(我尝试使用maven jar插件,但它没有添加依赖项)
插件
<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<archive>
<manifest>
<mainClass>com.dca.engine.StartDCA</mainClass>
</manifest>
</archive>
<packagingExcludes>WEB-INF/lib/jetty*.jar,WEB-INF/lib/org.apache.taglibs.standard.glassfish*.jar,WEB-INF/lib/org.apache.jasper.glassfish*.jar,WEB-INF/lib/org.eclipse.jdt.core*.jar,WEB-INF/lib/javax.servlet.jsp*.jar,WEB-INF/lib/javax.el*.jar</packagingExcludes>
<escapeString>\</escapeString>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.dca.engine.StartDCA</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.java</include>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
Run Code Online (Sandbox Code Playgroud)
嵌入式Jetty
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>8.1.10.v20130312</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>8.1.10.v20130312</version>
<!-- <scope>provided</scope> -->
</dependency> …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Java 7和Jetty 9.1.3维护一个使用嵌入式jetty独立运行的java Web应用程序.除了其中一个JSP页面中的所有链接外,其他所有内容都会运行.该页面上的每个链接都应该获取png文件或文本文件,并将其显示在链接下方.通过Netbeans运行时,每个链接在单击时都会挂起,并且控制台会出现以下错误:
Apr 10, 2014 4:23:34 PM org.apache.struts.chain.commands.AbstractExceptionHandler execute
WARNING: Unhandled exception
java.lang.IllegalStateException: Form too many keys
at org.eclipse.jetty.util.UrlEncoded.decodeUtf8To(UrlEncoded.java:526)
at org.eclipse.jetty.util.UrlEncoded.decodeTo(UrlEncoded.java:625)
at org.eclipse.jetty.server.Request.extractParameters(Request.java:344)
at org.eclipse.jetty.server.Request.getParameterNames(Request.java:852)
at org.apache.struts.util.RequestUtils.populate(RequestUtils.java:426)
at org.apache.struts.chain.commands.servlet.PopulateActionForm.populate(PopulateActionForm.java:50)
at org.apache.struts.chain.commands.AbstractPopulateActionForm.execute(AbstractPopulateActionForm.java:60)
at org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
at org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:304)
at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
at org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:711)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:552)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:568)
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:221)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1112)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:479)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:183)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1046)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
at org.eclipse.jetty.server.handler.HandlerList.handle(HandlerList.java:52)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
at org.eclipse.jetty.server.Server.handle(Server.java:462)
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:281)
at …Run Code Online (Sandbox Code Playgroud) 我正在玩Jetty GzipHandler,它似乎工作得相当奇怪:它只压缩已经压缩的文件.
我的整个设置是
GzipHandler gzipHandler = new GzipHandler();
gzipHandler.setHandler(myHandler);
server.setHandler(gzipHandler);
Run Code Online (Sandbox Code Playgroud)
浏览器(Chromium)始终发送包含的标头
Accept-Encoding:gzip,deflate,sdch
Run Code Online (Sandbox Code Playgroud)
所以根据文件
GZIP Handler如果出现以下情况,此处理程序将gzip响应的内容:
- 过滤器映射到匹配路径
- 响应状态代码> = 200且<300
- 内容长度未知或大于minGzipSize initParameter或minGzipSize为0(默认值)
- content-type在mimeTypes initParameter中设置的逗号分隔的mimeTypes列表中,或者如果没有定义mimeTypes,则content-type不是"application/gzip"
- 资源未指定内容编码
它应该适用于两者.我只是不确定这path部分,但没有指明任何部分,我希望它适用于两者或两者都不适用.
我曾经window.location.reload(true)强行重装.标题相当长,所以我将它们链接起来:css和png.
我试图设置一些属性,但没有任何成功.如果我找到
jetty-servlets-9.1.3.v20140225-sources.jar,我会调试它.问题是:为什么GzipHandler决定只压缩压缩文件?它是完全确定的:jpg并且png被压缩(无论多小)并且没有其他文件.
通过setMimeTypes我可以排除图像.我调试它,我仍然没有线索,为什么其他静态资源永远不会被压缩.我仔细检查了myHandler它们是否均匀地对待它们(它们都是直接从预先计算出来的Map<String, byte[]>).
在服务器(嵌入式Jetty)上,我需要重定向到其他端口,而其他所有内容均保持不变,例如,从
http://com.example.myserver:1234/whatever?with=params#and-hash?and=whoknowswhat
Run Code Online (Sandbox Code Playgroud)
至
http://com.example.myserver:5678/whatever?with=params#and-hash?and=whoknowswhat
Run Code Online (Sandbox Code Playgroud)
看来我必须根据我不太了解的内容来组成结果URL:
我正在使用spring-boot进行som实验,并且我意识到当我使用嵌入式Tomcat服务器时,生成的WAR大小比我使用Jetty甚至是具有相同其他依赖项的Undertow服务器时要小.
这怎么可能?...与tomcat相比,Undertow和Jetty应该是超轻型的.
尺码为:
Tomcat~18Mb
承诺~21Mb
码头~24Mb
它们中的任何一个对我来说都太大了,因为这是虚拟REST端点.这些是我的依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- <dependency> -->
<!-- <groupId>org.springframework.boot</groupId> -->
<!-- <artifactId>spring-boot-starter-tomcat</artifactId> -->
<!-- </dependency> -->
<!-- <dependency> -->
<!-- <groupId>org.springframework.boot</groupId> -->
<!-- <artifactId>spring-boot-starter-undertow</artifactId> -->
<!-- </dependency> -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<!-- <dependency> -->
<!-- <groupId>org.springframework.boot</groupId> -->
<!-- <artifactId>spring-boot-starter-test</artifactId> -->
<!-- <scope>test</scope> -->
<!-- </dependency> -->
</dependencies>
Run Code Online (Sandbox Code Playgroud) 我正在开发一个启动嵌入式Jetty服务器的Spring应用程序.然后它将Spring MVC Web应用程序"部署"到此Jetty服务器.
一切都适用于多个控制器,但我无法将Spring Security添加到Web应用程序中.我使用基于编程和注释的配置,Jetty服务器配置如下:
Server server = new Server(8080);
server.setStopAtShutdown(true);
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.setConfigLocation("com.mypackage.web");
context.setParent(mainContext);
ServletContextHandler contextHandler = new ServletContextHandler();
contextHandler.setErrorHandler(null);
contextHandler.setContextPath("/");
DispatcherServlet dispatcherServlet = new DispatcherServlet(context);
DefaultServlet staticServlet = new DefaultServlet();
contextHandler.addServlet(new ServletHolder(dispatcherServlet), "/");
contextHandler.addServlet(new ServletHolder("staticServlet", staticServlet), "/res");
contextHandler.addEventListener(new ContextLoaderListener(context));
contextHandler.setResourceBase("webapp");
server.setHandler(contextHandler);
Run Code Online (Sandbox Code Playgroud)
我还创建了一个类com.mypackage.web.SecurityConfig,它扩展WebSecurityConfigurerAdapter并覆盖configure方法,如下所示:
@Configuration
@EnableWebMvcSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin().and()
.httpBasic();
}
}
Run Code Online (Sandbox Code Playgroud)
据我了解文档,这应该足以"锁定"我的应用程序.当我在调试模式下启动应用程序时,在configure方法中遇到一个断点,因此Spring似乎检测到了配置类.
但是,我仍然可以访问我的应用程序中的任何页面,而无需重定向到默认登录表单.
我是否需要告诉Jetty Servlet容器有关此配置或我错过了其他内容?
对于这个问题,我准备了一个测试项目WssEmbedded,该项目在localhost:8080和侦听传入的WebSocket连接localhost:8443。
在MyHandler类中,我为此创建了2个连接器:
public class MyHandler extends WebSocketHandler {
@Override
public void configure(WebSocketServletFactory factory) {
factory.register(MyListener.class);
}
public static void main(String[] args) throws Exception {
SslContextFactory sslContextFactory = new SslContextFactory();
sslContextFactory.setKeyStorePath("keystore.jks");
sslContextFactory.setKeyStorePassword("OBF:1l1a1s3g1yf41xtv20731xtn1yf21s3m1kxs");
Server server = new Server();
server.setHandler(new MyHandler());
ServerConnector wsConnector = new ServerConnector(server);
wsConnector.setHost("127.0.0.1");
wsConnector.setPort(8080);
server.addConnector(wsConnector);
ServerConnector wssConnector = new ServerConnector(server,
new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()));
wssConnector.setHost("127.0.0.1");
wssConnector.setPort(8443);
server.addConnector(wssConnector);
server.start();
server.join();
}
}
Run Code Online (Sandbox Code Playgroud)
我添加了一个密钥/证书对keystore.jks:
keytool -genkey -alias key1 -keyalg RSA -keypass password1 …Run Code Online (Sandbox Code Playgroud) embedded-jetty ×10
java ×6
jetty ×6
servlets ×2
spring ×2
websocket ×2
compression ×1
handler ×1
html ×1
http ×1
jetty-9 ×1
jsp ×1
maven ×1
spring-boot ×1
spring-mvc ×1
ssl ×1
tomcat ×1
undertow ×1