I just tried to create the login form using html in servlet.but I got stuck some place.
I'm just using maven dependencies.so I don't have to care about jars.
so,let's take a look my main java code
package tutor.programacion.primerservlet;
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 j2html.tags.Tag;
import static j2html.TagCreator.*;
/**
* Servlet implementation class sampleJava2HTML
*/
@WebServlet("/")
public class sampleJava2HTML extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("inside the servlet calling this");
enterPasswordInput("please enter something");
}
public static Tag enterPasswordInput(String placeholder) {
return passwordInput("enterpassword",placeholder);
}
public static Tag passwordInput(String identifier,String placeholder) {
return input()
.withType("password")
.withId(identifier)
.withName(identifier)
.withPlaceholder(placeholder)
.isRequired();
}
}
To generate the HTML you should add your Tag to a ContainerTag like
html()
and then callrenderFormatted()
method.For example:
Since you are on a Servlet class, instead writing on console (System.out.println()) you should write to your response on doGet method as bellow:
This should render the following HTML:
My project was built with maven using j2html version 1.2.2