1

With the below servlet code,

package com.example.tutorial;

import java.io.IOException;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class ServletExample extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        if(request.getParameter("firstname") == null || request.getParameter("lastname") == null){
            this.getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
            return;
        }

this.getServletContext().getRequestDispatcher("/output.jsp").forward(request, response);
    }

}

I first receive the request parameters using index.jsp as shown below:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</head>
<body>
    <form action="servletexample" method="post" >
       <table border="0">
         <tr>
            <td>First Name:</td> <td><input type="text" name="firstname" /></td>
         </tr>
         <tr>
            <td>Last Name:</td> <td><input type="text" name="lastname"  /></td>
         </tr>
         <tr>
            <td colspan="2"> <input type="submit" value="Submit" /></td>
         </tr>
       </table>
    </form>
</body>
</html>

and then the servlet communicate with output.jsp using requestdispatcher, as shown below:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</head>
<body>
    <h1>Your first and last name is: </h1>
    <%

        String firstName = request.getParameter("firstname");
        String lastName = request.getParameter("lastname");

        out.print(firstName + " " + lastName);
    %>

</body>
</html>

In the above output.jsp code, request.getParameter method is used to retrieve the forwarded parameters from servlet.

Now, instead of output.jsp, I would like to use only html file[with javascript(if required)],

What should be the content of html body?

Note: Going further, Intention is to just use html/javascrip/css as front end and java based web frameworks(like Spring) as back end. My current learning about servlets/jsp is to have smooth transition to Spring framework(backend) that can work with html/javascrip/css

overexchange
  • 15,768
  • 30
  • 152
  • 347
  • 2
    Use ajax requests instead and use JSON to write the response rather than forward. Then, your front end may be pure HTML or another technology like a mobile app. – Luiggi Mendoza Jun 01 '15 at 16:22
  • @JarrodRoberson I'm not quite sure if that's a direct duplicate since OP states at the bottom of the question that he/she doesn't want to use JSP but html/javascript/css. While that question explicitly explains how to remove scriptlets (and avoid the ugly example OP provided because what decent programmer could use a servlet as a by pass only for communicating two JSPs), it still needs some server side processing of the data like Expression Language and custom tags e.g. JSTL. – Luiggi Mendoza Jun 01 '15 at 16:24
  • @LuiggiMendoza Am new to web programing, I currently knew servlet and jsp as of now. If I learn front end programming using javascript, there should be backend logic(like servlet) to process and place in DB(if required) – overexchange Jun 01 '15 at 16:27
  • Your question is a bit unclear. The plain answer is "it doesn't". Basically, an HTML page is just a description of things to be displayed in a browser. So either there are Javascript actions in it that communicate with the server and change the display, or new HTML is created by the server and sent over as a response to a request. Which scenario do you want? – RealSkeptic Jun 01 '15 at 16:28
  • Well, now you've learn something you should never do with Servlets and JSP, even if you can. – Luiggi Mendoza Jun 01 '15 at 16:28
  • @RealSkeptic I want, Javascript actions that communicate with the server and change the display. Basically I want to see html/JavaSript/css as frontend and servlet(only) as backend – overexchange Jun 01 '15 at 16:30
  • Then indeed, the first comment by Luiggi Mendoza is correct. You need to write servlets that just supply the information the Javascript actions need, and the HTML page should be written with AJAX calls to those servlets, and Javascript code that interprets them and changes stuff on the page. This is a very broad subject and you should do some research into it on your own. – RealSkeptic Jun 01 '15 at 16:34
  • @LuiggiMendoza - then pick one you like, this is not only a **duplicate** of at least 6 different questions because it is **too broad** as well. –  Jun 01 '15 at 16:44
  • @JarrodRoberson I don't see it too broad. The answer is straighforward: open a request using JavaScript (ajax request will be great in this case), get the response from the server, handle it in the front end. Explaining each step in detail is outside of the answer since looks like OP knows how to do this. – Luiggi Mendoza Jun 01 '15 at 16:46
  • @RealSkeptic Is this the good resource to start?https://www.youtube.com/watch?v=_iRIPJHrdXg&list=PL3877C5434C042349 am new to this, I need guidance, on how to think of using html/javascript/css as front end and java based webframework(like spring) as back end. Before going to srping, I had been thru servlets/jsp for smooth transition – overexchange Jun 01 '15 at 16:47
  • 1
    @overexchange Spring will use a Servlet behind the scenes for you. Learning to work with servlets to *smoothly* change to use Spring is not a good idea, since when using Spring you will rewrite lot of things. It is fine reviewing servlets for learning purposes, but it's not necessary if you're going to use Spring in the long term. – Luiggi Mendoza Jun 01 '15 at 16:54
  • @LuiggiMendoza Do I need to learn hibernate/struts/webservices before I go for "spring nature" projects? I am good in Core java. – overexchange Jun 02 '15 at 11:18
  • No, you don't need any of that. And struts is an mvc framework for presentation purposes, it doesn't fit your needs. I recommend you to re read my answer again. For communication with database there are many alternatives, you may use plain jdbc, spring jdbc, hibernate, jpa, mybatis... And on. It will depend on you to decide which one to use. – Luiggi Mendoza Jun 02 '15 at 13:31
  • @LuiggiMendoza After I read your answer again, I would like to say, this is one possibility: "javascript/html/css will be part of presentation logic(frontend) and the http requests will be passed to backend using ajax/jquery and in the back end, tomcat server running spring framework project responds back to browser using JSON." Is this meaningful statement? – overexchange Jun 02 '15 at 14:47

2 Answers2

1

From your comment:

I want, Javascript actions that communicate with the server and change the display. Basically I want to see html/JavaSript/css as frontend and servlet(only) as backend

Then it will be better to use ajax requests instead and use JSON to write the response rather than forward. Then, your front end may be pure HTML or another technology like a mobile app.

Note that if the only intend for you to use Servlets is to create RESTful services, then you will be better to use a REST approach rather than writing servlets on your own. For instance, you hace two options:

  • Use JAX-RS. You can choose an implementation like jersey or resteasy
  • Use Spring. This framework offers many capabitilies (many people will say they are benefits, it will depend on your point of view, though), and one of them is creating RESTful services. Here's a tutorial for creating the services using Spring Boot: https://spring.io/guides/gs/rest-service/.

If you're new to Java at all, I would recomment you to use JAX-RS. If you already have some experience with Java and Spring, then I recommend you using Spring, using Spring Boot it's an option, it will depend on your specific case.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • I have changed my note secion in the query, that would help you know my intentions!! Sorry that somebody has downvoted you!!! – overexchange Jun 01 '15 at 16:53
  • Let me first get orientation on Spring framework and then get back to, "how to" stuff with javascript? then I will try understanding this answer. – overexchange Jun 01 '15 at 17:12
0

First java is compiler based language and html is interpreter based language.so you cannot write java code inside the html file.it can be written in jsp. If you want to use html with out writing java code in it you can.

create html file output.html by using this code.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
 <h1>Your first and last name is: </h1>


</body>
</html>

and after this just replace the statement of RequestDispatcher forward statement by this

this.getServletContext().getRequestDispatcher("/output.html").forward(request, response);