Use javac command to compile the Servlet, and place it in the running tomcat

  1. First, I created a new txt file on your desktop, edit the content (content from rookie tutorial) as follows:
// import of java libraries required 
Import the java.io. * ;
 Import the javax.servlet *. ;
 Import the javax.servlet.http *. ; 

// extend HttpServlet class 
public  class the HelloWorld the extends HttpServlet { 
 
  Private String Message; 

  public  void the init () throws ServletException 
  { 
      // perform the required initialization 
      Message = "the Hello World" ; 
  } 

  public  void the doGet (the HttpServletRequest Request, 
                    the HttpServletResponse Response) 
            throwsServletException, IOException 
  { 
      // set the response content type of 
      the response.setContentType ( "text / HTML" ); 

      // actual logic here is 
      the PrintWriter OUT = response.getWriter (); 
      Out.println ( "<h1 of>" Message + + "</ h1 of>" ); 
  } 
  
  public  void the destroy () 
  { 
      // do nothing 
  } 
}

Save, name the file HelloWorld (and the main class name the same), the file suffix .java.

  2. Then hold down the shift key, right-click the desktop space, select "Open PowerShell window here", enter the command javac compilation, given as follows.

Solution: javac has a encoding parameter, then changed javac -encoding utf-8 \ HelloWorld.java, execute the command again, the following error.

Solution: The original dependent jar package did not lead to go, how to import it? In an environment variable - under> System variables, the new (if not) variable CLASSPATH, select "Browse File" to find jar file (you can use the search Everything jar package location) on the desktop.

Executed again, or error, re-open PowerShell, compile successfully.

  3. 把生成的class文件放到tomcat目录如E:\Program Files\apache-tomcat-7.0.68下的webapps\ROOT\WEB-INF\classes文件夹下,我发现WEB-INF下没有classes文件,便新建了一个。

同时修改WEB-INF下的web.xml文件,在<web-app>标签中添加如下代码:

    <servlet>
        <servlet-name>HelloWorld</servlet-name>
        <servlet-class>HelloWorld</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>HelloWorld</servlet-name>
        <url-pattern>/HelloWorld</url-pattern>
    </servlet-mapping>

  4. 保存后,到tomcat目录下的bin文件下找到startup.bat双击运行。

等到出现如下信息,表示启动成功,注意http端口号,一般默认为8080。

  5. 最后就可以在浏览器上输入localhost:8080/HelloWorld访问servlet了

 

Guess you like

Origin www.cnblogs.com/Meiwah/p/11256555.html