Configure tomcat in netbeans

Source: Internet
Author: User
Tags netbeans
1. Add a Tomcat server under netbeans-tools-server Manerger. By default, the Tomcat server is configured.
  • Configuration User: this is to be set in the tomcat-users.xml file under base Directory
  • Role: Admin, manager, or admin
  • Home Directory.The directory where the server is located. After the server is installed, it does not matter.
  • Base directory:User Configuration directory, set your server and your servlets
2. Create a project by publishing the servlet, and Tomcat servlet example under samples. Name it atatservletexample
  • Apache-tomcat-5.5.17_base/work/Catalina/localhost/Servlets-examples/tldcache. Ser under base Directory
  • Depth # examples. xml under apache-tomcat-5.5.17_base/CONF/Catalina/localhost:

This structure is published in the form of folders and files. The docbase attribute in the servlets-examples.xml file mentioned above is the directory of my project file, path is the access path developed, for example, I can use http: // localhost: 8084/Servlets-examples/access, client access is directed through this file

The typical structure of docbase is:

*. Html, *. jsp, etc:Page /WEB-INF/Web. xml:He Web application deployment descriptorFor your application. this is an XML file describing the servlets and other components that make up your application, along with any initialization parameters and container-managed security constraints that you want the server to enforce for you. /WEB-INF/classes /:Compiled. Class File /WEB-INF/lib/: This directory contains jar files that contain Java class files (and associated resources) required for your application, such as third party class libraries or JDBC drivers. 3. find out the structure, and then write a simple one by yourself
  • Create a new Web application with index. jsp as the home page by default. The home page can be modified under the pages tab of Web. xml.
  • Add index. jsp under the Body Tag:


Execute
  • Add newservlet. Class under source packages and add the code to the end
  • Configure newservlet under the Web. xmlservlet label and specify the servlet class and URL pattern. What I specify is/newservlet, which is accessed under http: // localhost: 8084/webservlettest/newservlet.
  • It is found that the correct results can be displayed only after compilation twice.
  • When connecting an image,The file name is case sensitive.
The example of Tomcat Servlet under the Web sample of netbeans is a good example of learning servlet.Instance code: ------------------------------------- import java. Io .*;
Import java.net .*;
Import javax. servlet .*;
Import javax. servlet. http. *; public class newservlet extends httpservlet {/** initializes the servlet. Connections to databases belong here!
*/
Public void Init (servletconfig config) throws servletexception {
Super. INIT (config );

}

/** Destroys the servlet. close connections, files, etc.
*/
Public void destroy (){

}

// Form values can be passed by 2 methods, get or post
// The doget () (resp. dopost () method is called when the method is
// Get (RESP post ).
// The following trick allows us to process both with one function

Protected void doget (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
Processrequest (request, response );
}

Protected void dopost (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
Processrequest (request, response );
}

Protected void processrequest (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
Response. setcontenttype ("text/html ");
Printwriter out = response. getwriter ();

Out. println ("");
Out. println ("");
Out. println ("");
Out. println ("");
Out. println (""); // the real work of the servlet begins here
 
// A servlet does nothing else than outputing HTML code
// To the webserver. It can output the HTML code corresponding
// To a form. The user fills this form and, when the submit button
// Is clicked, the values are sent to the appropriate program on
// Webserver (in this case, our servlet) which then uses these values
// To 'calculate' what it shoshould display this time
 
// If the servlet is simply
// Called by visiting an URL or clicking a link, all parameters
// Will have null values. This is what happens when you type
// 'Www .google.com 'in your browser. We can detect this and
// Print out a default 'Welcome 'message (as below ).
// If the servlet is called by clicking a submit
// Button, no parameter will have null values (fields not filled
// By the user will return empty strings, not null ).
 
If (request. getparameter ("myparam ")! = NULL)
// The request. getparameter function allows us to obtain
// Values entered by the user in the various input fields
Out. println ("Your parameter is" + request. getparameter ("myparam") +"
");
Else
Out. println ("hello, please enter a parameter!
");

Out. println ("Enter your new parameter here:
");
Out. println (
// The 'action' field of the 'form' tag indicates which program
// Shocould be called by the webserver when the user clicks 'submit'
// In this case, we tell it to call the same servlet again
"" +
// The 'name' of the input field corresponds to the name of
// Parameter which will contain the value
// Entered in this input field (here, it is 'myparam'-see abve)
"
"+
// The Special 'submit 'input field generates a submit button
""
// When the user clicks the submit button, the browser sends
// Request to the servlet whose name is contained in the 'Action'
// Field of the ''tag, which in this case is the present
// Servlet. This request has des the values entered by the user
// In the different input fields as parameters.
+ ""
);

Out. println ("");
Out. println ("");

Out. Close ();
}

Public String getservletinfo (){
Return "Short Description ";
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.