package com.bisht.demos;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
@WebServlet(name = "ShowRequestHeaders")
public class ShowRequestHeaders extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println("<!Doctype HTML>");
out.println("<html>");
out.println("<title>Showing all Request Headers</title>");
out.println("<head>");
out.println("<body>");
out.println("<h1>All Request Headers </h1>");
out.println("<p><b>Request Method:</b>"+request.getMethod());
out.println("<p><b>Request URRL :</b>"+request.getRequestURL());
out.println("<p><b>Request Protocol:<b>"+request.getProtocol());
Enumeration<String> headerNames=request.getHeaderNames();
while(headerNames.hasMoreElements()){
String headerName=headerNames.nextElement();
out.println("<p><b>"+headerName+":</b>"+request.getHeader(headerName)+"</p>");
}
out.println("</body>");
out.println("</html>");
out.close();
}
}
当我在具有适当URL的Web浏览器上运行此代码时,我会看到成功执行以下网页:-http://localhost:8080/ShowRequestHeaders
但是当我尝试在命令行中执行servlet程序时,它显示了错误!这是这里的:-Command line image here
非常感谢!
Microsoft Windows [Version 10.0.17134.165]
(c) 2018 Microsoft Corporation. All rights reserved.
C:\Users\Akash\IdeaProjects\yoyo\src\com\bisht\demos>set path=C:\Program Files\Java\jdk1.8.0_181
\bin
C:\Users\Akash\IdeaProjects\yoyo\src\com\bisht\demos>javac SetStatusDemo.java
SetStatusDemo.java:3: error: package javax.servlet does not exist
import javax.servlet.ServletException;
^
SetStatusDemo.java:4: error: package javax.servlet.annotation does not exist
import javax.servlet.annotation.WebServlet;
^
SetStatusDemo.java:5: error: package javax.servlet.http does not exist
import javax.servlet.http.HttpServlet;
^
SetStatusDemo.java:6: error: package javax.servlet.http does not exist
import javax.servlet.http.HttpServletRequest;
^
SetStatusDemo.java:7: error: package javax.servlet.http does not exist
import javax.servlet.http.HttpServletResponse;
^
SetStatusDemo.java:11: error: cannot find symbol
public class SetStatusDemo extends HttpServlet {
^
symbol: class HttpServlet
SetStatusDemo.java:10: error: cannot find symbol
@WebServlet(name = "SetStatusDemo")
^
symbol: class WebServlet
SetStatusDemo.java:12: error: cannot find symbol
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws Servl
etException, IOException {
^
symbol: class HttpServletRequest
location: class SetStatusDemo
SetStatusDemo.java:12: error: cannot find symbol
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws Servl
etException, IOException {
^
symbol: class HttpServletResponse
location: class SetStatusDemo
SetStatusDemo.java:12: error: cannot find symbol
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws Servl
etException, IOException {
^
symbol: class ServletException
location: class SetStatusDemo
SetStatusDemo.java:16: error: cannot find symbol
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws Servle
tException, IOException {
^
symbol: class HttpServletRequest
location: class SetStatusDemo
SetStatusDemo.java:16: error: cannot find symbol
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws Servle
tException, IOException {
^
symbol: class HttpServletResponse
location: class SetStatusDemo
SetStatusDemo.java:16: error: cannot find symbol
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws Servle
tException, IOException {
^
symbol: class ServletException
location: class SetStatusDemo
SetStatusDemo.java:18: error: cannot f
ind symbol
response.setStatus(HttpServlet
Response.SC_MOVED_TEMPORARILY);
^
symbol: variable HttpServletResponse
location: class SetStatusDemo
14 errors
C:\Users\Akash\IdeaProjects\yoyo\src\com\bisht\demos>
非常感谢!