Configuring Tomcat in Eclipse
Create and run servlet/jsp
Step One: configure Tomcat in eclipse (note download eclipse IDE for Java EE developers)
(1) inEclipsein the configurationTOmcat. ChooseWindow→preferencescommand, in the left list box in the Open dialog box, selectServerin the nodeRuntime Environments. On the right side of the window, clickADDbutton to openNew Server Runtime environmendialog box, where you can select the type and version of the server, using theApache Tomcat v 7.0.
(2) Select File→new→dynamic web Project in Eclipse to open the new dynamic Web Project dialog box. In the project name text box, enter the name of the item, such as helloweb, and the following options take the default values.
(3)ClickNextbutton to openWeb Moduledialog box, where you need to specifyWebApplication Context root directory name andWebcontent to be stored in the directory, the default value is used here, selectGenerate Web Deployment Descriptorcheck box, byEclipsegenerates a deployment profile,1-2is shown. Last ClickFinishbutton to end the creation of the project.
Figure 1-2 Web Module dialog box
Step two: Use the Eclipse IDE to Create and run the Servlet.
(1 ) Right-click helloweb project, select from the pop-up menu new →servlet, open create servlet dialog box. In java package Enter the package name in the text box, such as com.demo class name Enter the class name in the text box helloservlet
(2 ) Click next button, go to the next dialog box. Here you need to specify servlet name and url servlet name modified to helloservlet url map name modified to Span style= "font-family: ' Times New Roman ';" >/helloservlet.do
(3) Click the Next button to specify the interface that the Servlet implements and the automatically generated method in the dialog box that appears. Finally click the Finish button,andEclipse will generate Some code for the Servlet and open it in the edit window. The complete code after the modification is as follows.
Package Com.demo;
Import java.io.IOException;
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.*;
@WebServlet (name = "HelloServlet", Urlpatterns = {"/helloservlet.do"})
public class HelloServlet extends HttpServlet {
Private static final long serialversionuid = 1L;
protected void doget (HttpServletRequest request,
HttpServletResponse response)
Throws Servletexception, IOException {
Response.setcontenttype ("Text/html;charset=utf-8");
PrintWriter out = Response.getwriter ();
Out.println ("
Out.println ("<body>
Out.println ("
Out.println ("The Present time is:" +new java.util.Date ());
Out.println ("</body>");
Out.println ("
}
}
(4) Right-click the Code section in the Eclipse IDE and select runas →run on Server in the popup menu to execute the Servlet.
Step three: create a JSP page using the Eclipse IDE .
(1 ) Right-click helloweb project node, select from the pop-up menu new →jsp file, open new jsp file dialog box. Select jsp page storage directory, here for webcontent. In file name text box enter a file name hello.jsp
(2) Click the Next button to open the Select JSP Template dialog box, select the template you want to use from the list of templates, select the New JSP File (HTML) template, and then click the Finish button. Eclipse creates a hello.jsp page and opens the file in the workspace, which you can <body> tag to insert the code.
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding= "UTF-8"%>
Simple JSP page </title>
<body>
The time now is:<%=new java.util.Date ()%>
</body>
(3) to run the JSP page, right-click in the JSP page editing area and select Run As→run on Server in the Open menu to execute the JSP page.
Configure Tomcat to create and run servlet/jsp in eclipse