0

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();
}

}
rama samy
  • 3
  • 1
  • Further explanation is required here, how are you stuck? – Rcordoval May 23 '18 at 06:23
  • @Rcordoval first of all thanks for the your response as per the requirement simply need to create the login view.I written the j2html code in my above servlet ,but when I check web page through my web browser completely blank it showing.in network tab response 200 ok shows – rama samy May 23 '18 at 06:32
  • @samy Have you tried to look at the source code of you blank page? Is there anything? – Tiago Mussi May 23 '18 at 14:48
  • Nothing is there ! I just press ctrl+U shortcut key it takes to view source page. – rama samy May 24 '18 at 06:07

1 Answers1

3

To generate the HTML you should add your Tag to a ContainerTag like html() and then call renderFormatted() method.

For example:

import static j2html.TagCreator.body;
import static j2html.TagCreator.form;
import static j2html.TagCreator.h1;
import static j2html.TagCreator.html;
import static j2html.TagCreator.input;

import j2html.tags.ContainerTag;
import j2html.tags.Tag;

public class _50480568 {

    public static void main(String[] args) {
        System.out.println(generateHTML());
    }

    private static String generateHTML() {
        return html(generateBody()).renderFormatted();
    }

    private static ContainerTag generateBody() {
        return body(generetaH1Title(), generateForm());
    }

    private static ContainerTag generateForm() {
        return form().withMethod("post").withAction("/yourServlet").with(generateUserField(), generatePasswordField());
    }

    private static Tag generateUserField() {
        return input().withType("text").withName("user").withId("user");
    }

    private static ContainerTag generetaH1Title() {
        return h1("Hello World - Body!");
    }

    private static Tag generatePasswordField() {
        return input().withType("password").withName("password").withId("password");
    }

}

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:

java.io.PrintWriter pw = resp.getWriter();
pw.println(generateHTML());

This should render the following HTML:

<html>
    <body>
        <h1>
            Hello World - Body!
        </h1>
        <form method="post" action="/yourServlet">
            <input type="text" name="user" id="user">
            <input type="password" name="password" id="password">
        </form>
    </body>
</html>

My project was built with maven using j2html version 1.2.2

<dependency>
    <groupId>com.j2html</groupId>
    <artifactId>j2html</artifactId>
    <version>1.2.2</version>
</dependency>
Tiago Mussi
  • 801
  • 3
  • 13
  • 20
  • I have two queries 1 is using j2html we can able to post and get the data? 2 is why we can't write the j2html code inside servlet? – rama samy May 24 '18 at 04:47
  • @ramasamy 1) j2html only prints HTML forms, if you create an HTML with form and action you can send user inputs to an servlet for example. 2) you can write j2html on a servlet, you just need to write on your page as the example above. I updated the example above using html form. – Tiago Mussi May 24 '18 at 06:59
  • it means printing in console ? it will not create dynamic html form ?I mean if write form code in html it will get run on web browser right ! like that j2html will work or not ? – rama samy May 24 '18 at 07:17
  • you're going to write on your response so it will render on a html page. try on your doGet method: resp.getWriter().println(generateHTML()); as the example above. – Tiago Mussi May 24 '18 at 07:21
  • can please suggest me some good tutorials for j2html.I am beginner to j2html – rama samy May 24 '18 at 07:59
  • i was looking on: https://j2html.com/examples.html Does your problem was solved? – Tiago Mussi May 24 '18 at 08:01
  • Thanks your for response renderFormatted() it's a built in method or programmer explicitly need to write it ! – rama samy May 24 '18 at 08:52
  • renderFormatted() is a method from ContainerTag.class on j2html api. It renders the ContainerTag (html in our case) and its childrens. – Tiago Mussi May 24 '18 at 09:00
  • I know it's ContainerTag class but mine html(generateBody()) after this I put (.) in eclipse throwing error over there and says render() method only available in ContainerTag class – rama samy May 24 '18 at 09:07
  • which version of j2html are you using? I'm using 1.2.2. Did you change your code or still the same on the example above? – Tiago Mussi May 24 '18 at 09:12
  • oops ! thanks I had used 1.0.0 may be that's the reason behind in it.I will update my version and check and let you know – rama samy May 24 '18 at 09:14
  • great. i will update my answer with the specific version I'm using. – Tiago Mussi May 24 '18 at 09:17
  • you suppose to call static method in your class within your servlet doGet() method,need to use className.method() right ? java.io.PrintWriter pw = resp.getWriter(); pw.println(generateHTML()); – rama samy May 24 '18 at 09:27