Definition of
Java Servlets Servlets are objects that are within the context of an application server or also known as servlet container (eg Tomcat) and extend its functionality.
The word derives from an earlier servlet, applet, which referred to small programs that run in the context of a web browser. In contrast, a servlet is a program that runs on a server.
The most common use servlets to generate pages web dynamically from the request parameters sent by the web browser. Tools needed
developing this Java Servlet using:
_ _ Netbeans IDE 6.8 Apache Tomcat 6.0.32
Basic Java Servlet Example
_ Our sample consists of 2 parts
* A Form: Who will run in the browser (client) and send data to a servlet.
* A servlet: Who will receive the form data and the client will return an HTML page with the data received. _
started Netbeans and select File \\ New Project .. _
opens a dialog which asks for the type of project, select Java Web \\ Web Application
_ In the next step, we asked the name of the project (Project Name ) In our case we entered Servlet_Encuesta (or whatever name you prefer)
The next step, we requested the application server (servlet container used). Thus, the combo Server, select Tomcat (according to version I have installed Tomcat 6.0.32)
_ Finally, we can select to use or frameworks (Spring, JSF, Struts or Hibernate). Do not select anything because, for now, for this example because it is not worth.
_ NetBeans creates his own project, a directory structure, and within the Web Pages folder a arhiva index.jsp, which will be the starting point for our application. While JSP extension, we do not write JSP code, but simply an HTML form . In the HTML form in the action attribute define the name of the servlet to execute the send (submit) the form. _
index.jsp File that is created by default is:
_ We will modify this file, with the following code: index.jsp
(Form)
\u0026lt;% @ page contentType = "text / html" pageEncoding = "UTF-8"%>
\u0026lt;! DOCTYPE HTML PUBLIC "- / / W3C / / DTD HTML 4.01 Transitional / / EN"
"http://www.w3.org/TR/html4/loose.dtd">
\u0026lt;html>
\u0026lt;head>
\u0026lt;meta http-equiv = "Content-Type" content = "text / html; charset = UTF-8">
\u0026lt;title> Send your opinion \u0026lt;/ title>
\u0026lt;/ head>
\u0026lt;body>
\u0026lt;h2> Please send us your opinion about web site \u0026lt;/ h2>
\u0026lt;form method="post"> action="ServletOpinion"
\u0026lt;p> Name: \u0026lt;input type="text" name="name" size="15"/> , \u0026lt;/ p>
\u0026lt;p> Surname: \u0026lt;input type="text" name="apellidos" size="30"/> \u0026lt;/ p> Opinion
which has earned this website
\u0026lt;input type="radio"
\u0026lt;p> checked name="opinion" value="buena"/> Good
\u0026lt;input type = "radio" name = " opinion "value =" regular "/> \u0026lt;input type="radio"
Regular Mala name="opinion" value="mala"/>
\u0026lt;/ p>
Comments
\u0026lt;p>
\u0026lt; ; textarea name = "comments" rows = "6" cols = "40"> \u0026lt;/ textarea>
\u0026lt;/ p> \u0026lt;input type="submit"
name="botonEnviar" value="Enviar">
\u0026lt;input type="reset" name="botonLimpiar" value="Limpiar">
\u0026lt;/ form>
\u0026lt;/ body>
\u0026lt;/ html>
Then, from the Project Explorer, then right-click the Source Packages folder, select New \\ Servlet ...
_ Here, she opens a dialog that asks us to name and servlet package.
_ On behalf, you must enter the same name attribute of the form action created above, then this will be the servlet to receive the data sent by the HTML form. In our case, as indicated in the form: ServletOpinion . _In
package can enter the name you want (the name used for the package was mypackage ) _
Given the name of the servlet and package, we click on Finish. _ Completed
this automatically creates a class with the given servlet name ( ServletOpinion for us), which inherits from HttpServlet . Also redefines (Override) some methods (doGet, doPost, getServletInfo) and filled with a bit of code. It also creates a method processRequest (invoked from methods doGet and doPost ) to process the forms to arrive by the methods GET and POST .
_ We, in this example, we will only complete with a few lines (as the majority of the NetBeans automatically completed) the method processRequest to create an HTML page that will be the response of the submitted form.
_ then modify the file with the following ServletOpinion.java
code
ServletOpinion.java (Servlet)
mypackage package;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet. http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
ServletOpinion
public class extends HttpServlet {
/ / Declaration for
member variables / / form fields
private String name = null;
private String surname = null;
private String review = null;
private String comments = null;
protected void processRequest (HttpServletRequest request, HttpServletResponse response) throws ServletException
,
IOException {response.setContentType ("text / html; charset = UTF-8");
/ / Obtaining the values \u200b\u200bof the object form travez
req name = request.getParameter ("name");
surname = request.getParameter ("surname");
opinion = request.getParameter ("opinion");
feedback = request.getParameter ("comments");
PrintWriter out = response.getWriter
();
try {out.println ("\u0026lt;html>");
out.println ("\u0026lt;head>");
out.println ("\u0026lt;title> Securities listed on Form \u0026lt;/ title>");
out.println ("\u0026lt;/ head>");
out.println ("\u0026lt;body>");
out.println ("Securities listed on the form \u0026lt;h1> \u0026lt;/ h1>");
out.println ("Name \u0026lt;p> \u0026lt; ; / b> "+ name +" \u0026lt;/ p> ");
out.println (" Last \u0026lt;p> : \u0026lt;/ B> "+ surname +" \u0026lt;/ p> ");
out.println (" \u0026lt;p> Opinion: \u0026lt;/ b> "+ opinion +" \u0026lt;/ p> ") ;
out.println ("\u0026lt;p> Comments: \u0026lt;/ b>" + comment + "\u0026lt;/ p>");
out.println ("\u0026lt;/ body>");
out . println ("\u0026lt;/ html>");
} finally {out.close ();
}}
@ Override protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException
,
IOException {processRequest (request, response);}
@ Override protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException
,
IOException {processRequest (request, response);}
@ Override public String
getServletInfo () {
return "This servlet reads data from a form" +
"and displays"}
}
Importante:
El archivo Configuration Files \ web.xml debe de incluir el nombre , la clase y el directorio donde se encuentra nuestro servlet, en nuestro caso este archivo debe de quedar asi :
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>ServletOpinion</servlet-name>
\u0026lt;servlet-class> mipaquete.ServletOpinion \u0026lt;/ servlet-class>
\u0026lt;/ servlet>
\u0026lt;servlet-mapping>
\u0026lt;servlet-name> ServletOpinion \u0026lt;/ servlet-name>
\u0026lt;url-pattern> / ServletOpinion \u0026lt;/ url-pattern>
\u0026lt;/ servlet-mapping>
\u0026lt;session-config>
\u0026lt;session-timeout>
30 \u0026lt;/ session-timeout>
\u0026lt;/ session-config>
\u0026lt; ; welcome-file-list> \u0026lt;welcome-file>
index.jsp \u0026lt;/ welcome-file>
\u0026lt;/ welcome-file-list>
\u0026lt;/ web-app>
* We must be careful with this file, because otherwise is properly configured, will bring us problems running your servlets.
Implementation of the project
_ Finally, we will implement the project, we can do it from the Run menu or by opening the context menu of the project (from the Project Explorer) and selecting Run.
_ When you run a web application with NetBeans, the first thing it is a deployable, something and distribute the application on the server. As much as our server is local and the NetBeans do transparent to us, we must understand that the Tomcat is running when you run the application and it also has a directory structure (different from our project folder structure) where the application stores running web, configuration files, packages, classes, etc. _
implementing the project will open the default browser to the page index.jsp (the one with the form.) _
We answer the questions on the form and click send, the data are sent to the server (Servlet)
_ The servlet receives this data, and customer returns an HTML page with the data received.
0 comments:
Post a Comment