100% found this document useful (1 vote)
426 views

Steps To Create Servlet Application in Netbeans IDE

The steps to create a servlet application in NetBeans IDE are: 1. Create a new Java Web project and give it a name. 2. Create a servlet class by right clicking on the source package and selecting new servlet. 3. Write code in the servlet class and an HTML file to display the output. The HTML form action must specify the servlet URL to submit data.

Uploaded by

Mithila Metri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
426 views

Steps To Create Servlet Application in Netbeans IDE

The steps to create a servlet application in NetBeans IDE are: 1. Create a new Java Web project and give it a name. 2. Create a servlet class by right clicking on the source package and selecting new servlet. 3. Write code in the servlet class and an HTML file to display the output. The HTML form action must specify the servlet URL to submit data.

Uploaded by

Mithila Metri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Steps to Create Servlet Application in Netbeans IDE

1. Open Netbeans IDE, Select File -> New Project

2. Select Java Web -> Web Application, then click on Next,

1. Give a name to your project and click on Next,


2. and then, Click Finish

3. The complete directory structure required for the Servlet Application will be created

automatically by the IDE.


4. To create a Servlet, open Source Package, right click on default packages -> New -

> Servlet.

5. Give a Name to your Servlet class file,


6. Now, your Servlet class is ready, and you just need to change the method definitions and you

will good to go.

7. Write some code inside your Servlet class.


// In doGet method we write the following code

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html");

PrintWriter out = response.getWriter();

String name = request.getParameter("name");

out.println("<HTML>");

out.println("<HEAD><TITLE>Hello, " + name + "</TITLE></HEAD>");

out.println("<BODY>");

out.println("Hello, " + name);

out.println("</BODY></HTML>");

8. Create an HTML file, right click on Web Pages -> New -> HTML
9. Give it a name.

10. Write some code inside your HTML file.

<body>

<FORM METHOD="GET" ACTION="MyServlet">

If you don't mind me asking, what is your name?

<INPUT TYPE=TEXT NAME="name"><P>

<INPUT TYPE=SUBMIT>

</FORM>

</body>

To make the form works with Java servlet, we need to specify the following attributes for

the <form> tag:

o method=”get” or “post” to send the form data as an HTTP GET or POST request to

the server. Generally, form submission should be done in HTTP GET or POST

method.

o action=”URL of the servlet”: specifies relative URL of the servlet which is

responsible for handling data posted from this form.

11. Check web.xml file. In the web.xml file you can see, we have specified the url-pattern and

the servlet-name,

<servlet>

<servlet-name>MyServlet</servlet-name>

<servlet-class>MyServlet</servlet-class>

</servlet>
<servlet-mapping>

<servlet-name>MyServlet</servlet-name>

<url-pattern>/MyServlet</url-pattern>

</servlet-mapping>

12. Run your application, right click on your Project and select Run

Output:

You might also like