我已经用Jetty如下创建了该服务器。 我还创建了一个侦听uri(/“ clientId”)
的servlet。现在这可以正常工作: http://localhost:8080/clientId
但是我希望它可以代替: http://localhost:8080/?clientId=3
如何使我的代码处理 /?clientId = 3 部分
我需要使用参数处理uri。
我需要添加/更改什么?
谢谢!
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandler;
public class ProtectingServer
{
public static void main(String[] args) throws Exception
{
org.eclipse.jetty.server.Server server = new org.eclipse.jetty.server.Server(8080);
ContextHandler context = new ContextHandler();
context.setContextPath("/clientId");
context.setResourceBase(".");
context.setClassLoader(Thread.currentThread().getContextClassLoader());
server.setHandler(context);
context.setHandler(new ClientHandler());
server.start();
server.join();
System.out.println();
}
}
ClientHandler
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class ClientHandler extends AbstractHandler {
@Override
public void handle(String target,
Request baseRequest,
HttpServletRequest request,
HttpServletResponse response) throws IOException
{
response.setContentType("text/html; charset=utf-8");
response.setStatus(HttpServletResponse.SC_OK);
PrintWriter out = response.getWriter();
String url = request.getParameter("url");
out.println("<h1>" + "HELLO" + "</h1>");
baseRequest.setHandled(true);
}
}
答案 0 :(得分:1)
您使用context.setContextPath("/clientId")
此设置的有效URL(并考虑您的ClientHandler
代码)将是...
http://localhost:8080/clientId/?url=myurlvalue