尝试以编程方式运行Web应用程序。编写了一个自动部署Web应用程序的代码。部署Web应用程序后,我使用URL
类通过一些输入调用servlet并打印响应,如下所示。
//code to start Tomcat server and deploy the web app
String myUrl = "http://localhost:8080/CrossSiteScripting?user=shri&passwd=firstPassword";
URL url = new URL(myUrl);
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setRequestMethod("GET");
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
String httpMethod = con.getRequestMethod();
while((inputLine=in.readLine()) != null){
response.append(inputLine);
}
in.close();
System.out.println("\n----------------------\nHTTP Method : "+ httpMethod +"\nOutput: "+response.toString());
我正在调用servlet的doGet()
方法。 doGet
方法中的代码如下所示。
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String user = request.getParameter("user");
String pwd = request.getParameter("passwd");
request.setAttribute("user", user);
request.setAttribute("passwd", pwd);
PrintWriter pw = response.getWriter();
pw.write("Inside doGet method of Servlet\nuser name: "+user+"password= "+pwd);
RequestDispatcher view = request.getRequestDispatcher("homePage.jsp");
view.forward(request, response);
}
如果我在doget
方法view.forward(request, response);
中注释掉最后一行,那么我就可以获得预期的响应。但如果没有注释,那么我得到NullPointerException
,服务器返回HTTP响应代码500
。
这是我在使用URL类运行应用程序时遇到的错误。
asAug 07, 2015 11:53:47 AM org.apache.catalina.core.StandardContext setPath
WARNING: A context path must either be an empty string or start with a '/' and do not end with a '/'. The path [/] does not meet these criteria and has been changed to []
C:\Users\IBM_ADMIN\workspace\Initiate Tomcat\.
Host: StandardEngine[Tomcat].StandardHost[localhost]
starting server...
Aug 07, 2015 11:53:47 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
Aug 07, 2015 11:53:47 AM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Aug 07, 2015 11:53:47 AM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Tomcat
Aug 07, 2015 11:53:47 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/8.0.23
Aug 07, 2015 11:53:48 AM org.apache.catalina.util.SessionIdGeneratorBase createSecureRandom
INFO: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [173] milliseconds.
Aug 07, 2015 11:53:48 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
started.
Initiating URL: http://localhost:8080/CrossSiteScripting?user=shri&user=manoj&user=shrikant&passwd=firstPassword&passwd=admino
Aug 07, 2015 11:53:48 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [CrossSiteScripting] in context with path [/] threw exception
java.lang.NullPointerException
at penetration.testing.CrossSiteScripting.doGet(CrossSiteScripting.java:41)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:575)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1521)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1478)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1177)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:857)
Exception in thread "main" java.io.IOException: Server returned HTTP response code: 500 for URL: http://localhost:8080/CrossSiteScripting?user=shri&user=manoj&user=shrikant&passwd=firstPassword&passwd=admino
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1638)
at main.java.launch.Main.main(Main.java:92)
由于view.forward()
方法,最后一行可能会抛出异常。哪个将控件转发到JSP.
还有其他方法吗?我希望JSP页面能够像在浏览器中打开时那样执行。
这是目录结构:
+InitiateTomcat
|-- +src
|---|- +main.java.launch
|---|--- main.java //the main class from where I start Tomcat server and deploy a web application
|---|- +penetration.testing
|---|--- CrossSitrScripting.java // the servlet which am calling
|-- +WebContent
+WEB-INF
homePage.jsp // the homepage
答案 0 :(得分:0)
500 http状态代码意味着服务器无法满足请求并且意外"意外"错误发生了,我猜那个页面" homePage.jsp"无法通过此URL访问,请尝试直接通过浏览器访问它以进行验证。
答案 1 :(得分:0)
您在CrossSiteScripting.java:41
你说的是:
view.forward(request, response);
因此,由此生成的视图为null,因为它无法找到homePage.jsp
RequestDispatcher view = request.getRequestDispatcher("homePage.jsp");
如果您的homePage.jsp
位于与当前页面相同的相对位置,那么您需要使用" homePage.jsp
"如果homePage.jsp
在根目录中,那么你应该使用正斜杠" /
"因此指向根" /homePage.jsp
"
我假设您应该在转发前使用正斜杠,同时转发如下:
RequestDispatcher view = request.getRequestDispatcher("/homePage.jsp");
答案 2 :(得分:0)
我猜错误或异常就在这里
RequestDispatcher view = request.getRequestDispatcher("homePage.jsp");
如果您直接编写homePage.jsp
,则访问homePage.jsp
之类的页面应为空字符串或以'/'开头,路径为:
C:\Users\IBM_ADMIN\workspace\Initiate Tomcat\homePage.jsp
所以你应该这样做
RequestDispatcher view = request.getRequestDispatcher("/homePage.jsp");
答案 3 :(得分:0)
无法通过URL直接访问WEB-INF目录中的任何内容。 请将homePage.jsp文件移到WEB-INF目录之外,然后尝试访问它。我希望它能奏效。
问候 和Sandeep