我正在查看Java中的Simple Servlet的示例代码。
在此示例中,Servlet由WEB-INF / web.xml文件条目
启动<servlet-class>com.mkyong.ServletDemo1</servlet-class>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>Servlet Name For Demo1</servlet-name>
<servlet-class>com.mkyong.ServletDemo1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Servlet Name For Demo1</servlet-name>
<url-pattern>/Demo1</url-pattern>
</servlet-mapping>
</web-app>
我可以在main()方法中的Java应用程序中启动(com.mkyong.ServletDemo1),调用方法。有可能吗?
感谢您的帮助。
答案 0 :(得分:5)
是的,这是可能的。您可以使用Jetty,一个轻量级的Servlet容器。可以将Jetty嵌入到您自己的应用程序中(此示例直接取自Jetty documentation):
public class MinimalServlets
{
public static void main( String[] args ) throws Exception
{
// Create a basic jetty server object that will listen on port 8080.
// Note that if you set this to port 0 then a randomly available port
// will be assigned that you can either look in the logs for the port,
// or programmatically obtain it for use in test cases.
Server server = new Server(8080);
// The ServletHandler is a dead simple way to create a context handler
// that is backed by an instance of a Servlet.
// This handler then needs to be registered with the Server object.
ServletHandler handler = new ServletHandler();
server.setHandler(handler);
// Passing in the class for the Servlet allows jetty to instantiate an
// instance of that Servlet and mount it on a given context path.
// IMPORTANT:
// This is a raw Servlet, not a Servlet that has been configured
// through a web.xml @WebServlet annotation, or anything similar.
handler.addServletWithMapping(HelloServlet.class, "/*");
// Start things up!
server.start();
// The use of server.join() the will make the current thread join and
// wait until the server is done executing.
// See
// http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#join()
server.join();
}
@SuppressWarnings("serial")
public static class HelloServlet extends HttpServlet
{
@Override
protected void doGet( HttpServletRequest request,
HttpServletResponse response ) throws ServletException,
IOException
{
response.setContentType("text/html");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().println("<h1>Hello from HelloServlet</h1>");
}
}
}
答案 1 :(得分:0)
要使用HTTPServlet,您需要像Tomcat这样的应用服务器。你不能简单地使用main方法。
答案 2 :(得分:0)
是的,有可能!!
在代码下面,您可以执行以实现在Java类的Main方法中运行Servlet
<强> MainClass.java 强>
import java.net.*;
import java.io.*;
public class MainClass
{
public static void main (String args[])
{
String line;
try
{
URL url = new URL( "http://localhost:8085/E/MyServlet?name=Ashok" );
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
line = in.readLine();
System.out.println( line );
in.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
<强> MyServlet.java 强>
import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;
public class MyServlet extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
PrintWriter out = res.getWriter();
out.println ("Hello " + req.getParameter("name") + ", this is SpecialServlet!");
out.close();
}
}
<强>的web.xml 强>
<?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"
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>E</display-name>
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/MyServlet</url-pattern>
</servlet-mapping>
</web-app>
将MyServlet.class
放入WEB-INF
- &gt; classes
文件夹。
然后运行MainClass.java
<强>输出强>
您好Ashok这是SpecialServlet!