In the last posts, we covered the basic concepts of Model2 and Java Servlet class. 

This post will explore more ways and functions to build programs. 

If you want to refer to my last post, please click the link below : 

2022.09.15 - [JSP] - JSP) Model1 vs Model2

2022.09.15 - [JSP] - JSP) Model2 - Java Servlet 1

 

Example 1 : Log in

LoginServlet.java

This program is processed only with the post method, so we don't need to fill out the doGet method.

There are two ways to forward the Java Servlet class to the JSP page:  Dispatcher method and Redirect method. 

1. Dispatcher method

RequestDispatcher dispatcher = request.getRequestDispatcher("dispatcher.jsp"); dispatcher.forward(request, response);

2. Redirect method

response.sendRedirect("redirect.jsp");

In this code, we will use the dispatcher method, which will also work with the redirect method. In the Example 2, we will discuss more about these methods.

package login;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
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 javax.servlet.http.HttpSession;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public LoginServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		PrintWriter out = response.getWriter();  // PrintWriter object

		String id = request.getParameter("id");
		String passwd = request.getParameter("passwd");
		if (id.equals("java") && passwd.equals("java")) {
			// Session object
			HttpSession session = request.getSession();
			session.setAttribute("id", id);		
			// 1. Dispatcher method 
			RequestDispatcher dispatcher = request.getRequestDispatcher("./servlet/ex666/loginSuccess.jsp");
			dispatcher.forward(request, response);
			
			// 2. Redirect method
//			response.sendRedirect("./servlet/ex666/loginSuccess.jsp");
		} else { 
			out.println("<script>");
			out.println("alert('Incorrect ID or password.')");
			out.println("history.back()");
			out.println("</script>");
		}
	}

}

index.html

With the <frameset> and <frame> tag, we can split the browser into two parts. 

<html>
<head>
<meta charset="utf-8">
<title>Index</title>
</head>
<frameset cols="30%,*">
	<frame src="/jsppro/servlet/ex666/menu.jsp" name="leftFrame" />
	<frame src="/jsppro/servlet/ex666/login.jsp" name="rightFrame" />
</frameset>
<body>

</body>
</html>

menu.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>

<html>
	<head>
	<meta charset="utf-8">
	<title>Menu</title>
	</head>
<%
	String id = (String)session.getAttribute("id");
%>
<body>
<%
	if(id == null){
%>
   <a href="login.jsp" target="rightFrame" />Log in</a>
<% 
	}
	else{
%>
	Welcome, <%=id %>!
<%
	}
%>

</body>
</html>

login.jsp

The action is connected to the annotation in LoginServlet.java.

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>

<html>
	<head>
	<meta charset="utf-8">
	<title>Log in</title>
	</head>
	<body>
	<form action="/jsppro/login" method="post">
		ID : <input type="text" name="id"/>
		Password : <input type="password" name="passwd"/><br>
		<input type="submit" value="login"/>
	</form>
</body>
</html>

loginSuccess.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>

<html>
	<head>
	<meta charset="utf-8">
	<title>Log in success</title>
	<script>
	top.leftFrame.location.href="/jsppro/servlet/ex666/menu.jsp";
	</script>
	</head>
	<body>
		Successfully logged in.
	</body>
</html>

You will see this result if you insert ID- java / Password-java. 

Example 2

In this example, we will learn more specifically about two methods for forwarding: Dispatcher method and Redirect method. With these example codes, compare each method.

redirect.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>

<html>
	<head>
	<meta charset="utf-8">
	<title>Insert title here</title>
	</head>
	<body>
	request Attribute value : <%=request.getAttribute("request") %>
	</body>
</html>

RedirectServlet.java

package send;

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;

/**
 * Servlet implementation class RedirectServlet
 */
@WebServlet("/RedirectServlet")
public class RedirectServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RedirectServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		request.setAttribute("request", "requestValue");
		response.sendRedirect("./servlet/ex777/redirect.jsp"); // forwarding 
	}

}

dispatcher.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>

<html>
	<head>
	<meta charset="utf-8">
	<title>Insert title here</title>
	</head>
	<body>
	request Attribute value : <%=request.getAttribute("request") %>
	</body>
</html>

DispatcherServlet.java

package send;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class DispatcherServlet
 */
@WebServlet("/DispatcherServlet")
public class DispatcherServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DispatcherServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub 
		
		request.setAttribute("request","requestValue"); 
		
		RequestDispatcher dispatcher = 
				request.getRequestDispatcher("./servlet/ex777/dispatcher.jsp");
		dispatcher.forward(request, response);
	}

}

 

+ Recent posts