servlet是否支持URL如下:
/xyz/{value}/test
Run Code Online (Sandbox Code Playgroud)
其中的价值可以用文字或数字代替.
如何在web.xml中映射?
我是编写Java Servlet的新手,我正在努力想要一个简单的HelloWorld例子来正常工作.
HelloWorld.java类是:
package crunch;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorld extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("Hello World");
}
}
Run Code Online (Sandbox Code Playgroud)
我正在运行Tomcat v7.0,并且已经阅读了类似的问题,响应涉及更改invoker servlet-mapping部分web.xml,本节实际上并不存在,当我添加它时仍然会出现同样的问题.
我的Java文件是:
public class MyClass {
public void method1() {
// some code
}
public void method2() {
//some code
}
public void method3() {
//some code
}
}
Run Code Online (Sandbox Code Playgroud)
在我的JSP页面中,我有三个HTML按钮.
如果我点击button1,那么只会method1被调用,如果我点击,button2那么只会method2执行,如果button3,然后只有method3,依此类推.
我怎样才能做到这一点?
我有以下servlet:
public class MyServlet extends HttpServlet {
private static final long serialVersionUID = 16252534;
private static int ping = 3000;
private Thread t;
private static boolean shouldStop = false;
@Override
public void init() throws ServletException {
super.init();
t = new Thread(new Runnable() {
@Override
public void run() {
while(!shouldStop) {
System.out.println("Now:" + System.currentTimeMillis());
try {
Thread.sleep(ping);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
t.start();
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
super.doGet(req, …Run Code Online (Sandbox Code Playgroud) 我有一个名为BookShopWeb的动态Web项目,我在eclipse中创建,具有以下目录结构
/BookShopWeb/|
|--src
|---WebContent
|
|---META-INF
|----WEB-INF---web.xml
|
|--css--styles.css
|--jsp---index.jsp
Run Code Online (Sandbox Code Playgroud)
在web.xml中,我将起始页面设置为
<welcome-file-list>
<welcome-file>/WEB-INF/jsp/index.jsp</welcome-file>
Run Code Online (Sandbox Code Playgroud)
在index.jsp中,我将css包括在内
<head>
<link rel="stylesheet" type="text/css" href="../css/styles.css" />
</head>
Run Code Online (Sandbox Code Playgroud)
加载时索引页面不会显示css信息.我使用firebug检查了元素,它显示错误报告
Apache Tomcat/6.0.29 - Error report..
The requested resource (/css/styles.css) is not available.
Run Code Online (Sandbox Code Playgroud)
知道为什么会这样吗?我怎么能纠正这个?谢谢你
我创建了一个Web应用程序项目.它包含一个servlet类和一个HTML表单.如何从HTML表单中调用servlet类?
在我的jsp中,如果我打电话<form action="/sampleServlet" method="get" name="form1">,我得到以下异常:
http 404错误 - 找不到sampleServlet.我在web.xml文件中设置sampleServlet,url-pattern也设置为/ sampleServlet.
为什么我得到404(找不到servlet.)?
我开始使用servlets在eclipse中开发一个java web应用程序,并在我的localhost上使用tomcat服务器进行测试.我已经在tomcat中部署了应用程序,但是当我尝试在浏览器中加载目标URL时,我得到以下堆栈跟踪:
Jul 31, 2013 2:58:31 PM org.apache.catalina.core.ApplicationContext log
INFO: Marking servlet ImageServlet as unavailable
Jul 31, 2013 2:58:31 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Allocate exception for servlet ImageServlet
java.lang.ClassNotFoundException: test.ImageServlet
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:527)
at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:509)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:137)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1144)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:865)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:136)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1852)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Run Code Online (Sandbox Code Playgroud)
ImageServlet类非常清楚地位于我的eclipse工作区的myproject/src/test文件夹中,其中myproject是eclipse项目的名称,test是包.
web.xml位于myproject/web/WEB-INF/web.xml中,myproject.xml位于myproject/myproject.xml
web.xml的内容是:
<?xml version="1.0"?> …Run Code Online (Sandbox Code Playgroud) 我知道这是一个非常常见的问题,因为我在包括SO在内的多个论坛中发现了许多与此相关的问题.但我还没有找到解决方案我的web.xml(位于WEB-INF)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>SMSProjectNew</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>ReceiveMessagesServlet</display-name>
<servlet-name>ReceiveMessagesServlet</servlet-name>
<servlet-class>com.sendreceive.ReceiveMessagesServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ReceiveMessagesServlet</servlet-name>
<url-pattern>/ReceiveMessagesServlet</url-pattern>
</servlet-mapping>
</web-app>
Run Code Online (Sandbox Code Playgroud)
html页面index.html,位于WebContent文件夹中
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
The application started successfully version 1:27
<form action="/ReceiveMessagesServlet" method="post">
<input type="text" name="number"/>
<input type="text" name="message"/>
<input type="submit" name="submit"/>
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
最后是servlet,ReceiveMessagesServlet,位于src\com.sendreceive包com.sendreceive中;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public …Run Code Online (Sandbox Code Playgroud) 我可以在不使用HTML表单的情况下从JSP文件调用servlet吗?
例如,在页面加载期间显示HTML表格中的数据库结果.