📜 ⬆️ ⬇️

What is a servlet and why do we need a portlet?

And so, dear friends, I plan to open a blog dedicated to portal technologies.
To warm up, I suggest you a short little story, after reading that, you will learn what a servlet is and why you invented a portlet. Little space, I'll write about everything briefly. Rewrite books, of which not one hundred have been released, I am not going to. I will write about common things, as well as about techniques that greatly simplify the life of the developer. If you want to know more, then http://java.sun.com/javaee/index.jsp is better.
hardly anything can be.
Get ready, you will experience the power of Java EE .

Intro


We write portlets on Java (Java) . What is a portlet? A portlet, in its essence, is a servlet.

Servlet


This class extends HttpServlet , which has two main methods
void doGet (HttpServletRequest request, HttpServletResponse response) {}
void doPost (HttpServletRequest request, HttpServletResponse response) {}
It is no secret that the browser can initiate two types of server requests: post (POST) and get (GET) .
As you may have guessed, the first method will work when a GET is requested for a servlet, the second is for a POST request.
You can override the third main method
void processRequest (HttpServletRequest request, HttpServletResponse response) {}
It will handle both the geth and the posts that come to the servlet.

We pull out the request parameters from the request, write the result in response. The response goes to the client’s browser.
You can get a specific parameter, a list of all names, a map (Map) .
Map- is an interface, its meaning is that it stores the key and the value associated with the key. In the case of query parameters, the key is the name of the parameter, the value is the parameter value (iron logic) .
For example, the user filled out the registration form and sent it to the server:
lolik.ru/registrationservlet?name=lolos&surname=lolobot&age=102
The parameter map will look like this:
name-> lols
age-> 102
surname-> lolobot
')
If it is interesting, we can talk about maps and other goodies of Java SE separately.
Requests can only be strings (String) values ​​that can be cast to the desired type. Obviously, the value of age is better to turn into an integer (int or Integer) .

Let's look at the method:
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("");
out.println("");
out.println("Servlet MyServlet");
out.println("");
out.println("");
out.println(" Servlet MyServlet at " + request.getContextPath () + " ");
out.println(" lols! ");
out.println("");
out.println("");
} finally {
out.close();
}
}

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("");
out.println("");
out.println("Servlet MyServlet");
out.println("");
out.println("");
out.println(" Servlet MyServlet at " + request.getContextPath () + " ");
out.println(" lols! ");
out.println("");
out.println("");
} finally {
out.close();
}
}

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("");
out.println("");
out.println("Servlet MyServlet");
out.println("");
out.println("");
out.println(" Servlet MyServlet at " + request.getContextPath () + " ");
out.println(" lols! ");
out.println("");
out.println("");
} finally {
out.close();
}
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("");
out.println("");
out.println("Servlet MyServlet");
out.println("");
out.println("");
out.println(" Servlet MyServlet at " + request.getContextPath () + " ");
out.println(" lols! ");
out.println("");
out.println("");
} finally {
out.close();
}
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("");
out.println("");
out.println("Servlet MyServlet");
out.println("");
out.println("");
out.println(" Servlet MyServlet at " + request.getContextPath () + " ");
out.println(" lols! ");
out.println("");
out.println("");
} finally {
out.close();
}
}

We take response, we push in it html and we send to the user.

Remark:
Thanks zer0access for correcting me. Following the link java.sun.com/javaee/5/docs/api/javax/servlet/http/HttpServlet.html you will see that there are more methods besides doGet and doPost.
The processRequest method is generated by a number of IDEs, for example, NetBeans 6.1. This is done as follows:
/**
* Handles the HTTP GET
method.
* param request servlet request
* param response servlet response
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Handles the HTTP POST method.
* param request servlet request
* param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

those. Having described the processRequest method, your doPost and doGet will work consistently. If there is a difference when calling doPost or doGet , then processRequest is unlikely to be used.
The service method first accepts the request, after the request is forwarded to the required doXxx . In service, you can do some general operations, for example, to make an entry in the database that such and such a user has addressed such a servlet, then the request is sent to the required servlet method for processing.
zer0access , thank you.

All servlets are good, but only one servlet can be on a page. Two servlets do not fit - too important people.
But what if you really want to place a servlet-calculator and a servlet-translator on one page?

Portlet


It is very simple - to write two portlets, one will count, the second - translate. Two, three, many portlets can be settled on one page. The classic Sunlet portlet has three modes: view , edit , help . The first one is basic, it is seen by the user. In the case of a calculator, buttons will be available in view mode. In edit mode, for example, you can set the type of calculator: normal or scientific, with sines, cosines and other tricky things. In help mode (help) ? As you may have guessed, there will be a help for the calculator.
View, edit, help modes are displayed using jsp (java server pages). They are very, very similar to php-pages:

<% for(int i=1; i<=10; i++){ %>
<%=i%>
<br>
<%}%>


Output in a column of numbers from 1 to 10 inclusive.

can you replace <%%> with <_? ?_> <_? ?_> (how is the code written here?) and there will be no difference (except for the fact that Java needs to define the types of variables).
With the view and help mode, everything is clear, but why do we need the edit mode? Suppose we have two user groups on the portal: the first is the accountants, the second is you, the programmers. Accountants have the usual calculator, where there is +, -, *, /, and we need to add binary numbers. In this case, the portal administrator for the group of accountants will configure the portlet as a normal calculator, and for our group as a scientific one.

Feel what a thrill?
A portlet, consider it as a small web application that can be placed on a portal page and customized (if implemented by a programmer) to the specific needs of the user. You can combine different portlets on the same page.

In the next issue we will talk about portlets in more detail. I'll tell you how you can set up an application server, a container for portlets in 5 minutes and how to debug on this entire economy with a single click.

Source: https://habr.com/ru/post/26316/


All Articles