servlets and JSP
So far we have been building REST APIs for the most part. Jersey and javax.ws.rs hide lot of happens behind the scenes.
Let's step our way back and take a look what else other than RESTful services can be done with your application.
If you remember the first lecture and our index.html file with "Hello World!". Where did we have to place it?
Let's create another static file in the Web Content folder
Create a new Dynamic Web project and associate it with Tomcat
Let's step our way back and take a look what else other than RESTful services can be done with your application.
If you remember the first lecture and our index.html file with "Hello World!". Where did we have to place it?
Let's create another static file in the Web Content folder
Create a new Dynamic Web project and associate it with Tomcat
This will load the file at the root of your project URL:
if you create a new directory under the Web Content and move file in the new directory, the path will change accordingly:
Simply put, JSP files follow rules of the static files in terms of placement and path, but are able to run java code, making them dynamic
TRYIT: Write and run your own JSP file
Including your classes in jsp
the java files and packages within the build path of the same application are accessible by your jsp pages:
servlets
Create a new Servlet
You should get a code generated that looks similar to:
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;
@WebServlet("/MyFirstServlet")
public class MyFirstServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public MyFirstServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getOutputStream().println("Hello World from Servlet");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
The annotation at the root of your servlet matches the location of your servlet: http://localhost:8080/unh2/MyFirstServlet
You can use HTML in the response output
To set response content type use: response.setContentType("text/html");
You can use HTML in the response output
To set response content type use: response.setContentType("text/html");
Sending data to servlet:
create an html file named submit.html with following content:
Which is now accessible at http://localhost:8080/unh2/submit.html
<html> <form method="GET" action="MyFirstServlet"> first name: <input type="text" name="firstName"><br/> last name: <input type="text" name="lastName"><br/> <input type="submit"> </form>that should create a form that looks like this:
Which is now accessible at http://localhost:8080/unh2/submit.html
You can now access the parameters sent by the form via request.getParameter:
response.setContentType("text/html"); String fName = request.getParameter("firstName"); String lName = request.getParameter("lastName"); PrintWriter pw = response.getWriter(); pw.println("<br/>got firstName:" + fName); pw.println("<br/>got lastName:" + lName);
full code:
package test; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/MyFirstServlet") public class MyFirstServlet extends HttpServlet { private static final long serialVersionUID = 1L; public MyFirstServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); String fName = request.getParameter("firstName"); String lName = request.getParameter("lastName"); PrintWriter pw = response.getWriter(); pw.println("<br/>got firstName:" + fName); pw.println("<br/>got lastName:" + lName); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }