By GokiSoft.com| 20:55 12/08/2020|
Web Component Development (SERVJSP)

[Share Code] Hướng dẫn tìm hiểu request, response, session, application trong JSP - jsp include, forward, param - JavaBean trong JSP/Servlet

[Share Code] Hướng dẫn tìm hiểu request, response, session, application trong JSP - jsp include, forward, param - JavaBean trong JSP/Servlet


#User.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package modals;

import java.io.Serializable;

/**
 *
 * @author Diep
 */
public class User implements Serializable{
    String name, email, birthday, password, confirmPwd, address;

    public User() {
    }

    public User(String name, String email, String address) {
        this.name = name;
        this.email = email;
        this.address = address;
    }

    public User(String name, String email, String birthday, String password, String confirmPwd, String address) {
        this.name = name;
        this.email = email;
        this.birthday = birthday;
        this.password = password;
        this.confirmPwd = confirmPwd;
        this.address = address;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getConfirmPwd() {
        return confirmPwd;
    }

    public void setConfirmPwd(String confirmPwd) {
        this.confirmPwd = confirmPwd;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "User{" + "name=" + name + ", email=" + email + ", birthday=" + birthday + ", password=" + password + ", confirmPwd=" + confirmPwd + ", address=" + address + '}';
    }
    
    
}


#error.jsp


<%-- 
    Document   : error.jsp
    Created on : Aug 12, 2020, 8:01:15 PM
    Author     : Diep
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <%
            //C1. Share du lieu qua bien moi truong application
            String msg = (String) application.getAttribute("msg");
            
            //C2. Share du lieu qua GET (param)
            String msg_param = request.getParameter("msg_param");
            String name_param = request.getParameter("name_param");
        %>
        <h1>Error!</h1>
        <%=msg%>
        <h1>Cach 2 : <%=name_param%></h1>
        <%=msg_param%>
        <h1>Cach hien thi noi dung</h1>
        <jsp:text>Noi Dung</jsp:text>
    </body>
</html>


#footer.jsp


<%-- 
    Document   : footer
    Created on : Aug 12, 2020, 7:45:00 PM
    Author     : Diep
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <h1>Footer</h1>
    </body>
</html>


#header.jsp


<%-- 
    Document   : header
    Created on : Aug 12, 2020, 7:44:44 PM
    Author     : Diep
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
	<title>Registation Form * Form Tutorial</title>
	<!-- Latest compiled and minified CSS -->
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"/>

	<!-- jQuery library -->
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

	<!-- Popper JS -->
	<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>

	<!-- Latest compiled JavaScript -->
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
    </head>
    <body>
        <!-- A grey horizontal navbar that becomes vertical on small screens -->
        <nav class="navbar navbar-expand-sm bg-light">

          <!-- Links -->
          <ul class="navbar-nav">
            <li class="nav-item">
              <a class="nav-link" href="#">Link 1</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="#">Link 2</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="#">Link 3</a>
            </li>
          </ul>

        </nav>
    </body>
</html>


#process.jsp


<%-- 
    Document   : process.jsp
    Created on : Aug 12, 2020, 7:50:57 PM
    Author     : Diep
--%>

<%@page import="modals.User"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <body>
        <jsp:include page="header.jsp"/>
        <h1>Processing ... </h1>
        <%
            String name = request.getParameter("name");
            String email = request.getParameter("email");
            String birthday = request.getParameter("birthday");
            String password = request.getParameter("password");
            String confirmation_pwd = request.getParameter("confirmation_pwd");
            String address = request.getParameter("address");
            
            User user = new User();
            user.setName(name);
            user.setAddress(address);
            user.setBirthday(birthday);
            
            application.setAttribute("user", user);
            
            String msg = "";
            if(name.isEmpty()) {
                msg += "<h2>Name is empty<h2>";
            }
            if(email.isEmpty()) {
                msg += "<h2>Email is empty<h2>";
            }
            if(birthday.isEmpty()) {
                msg += "<h2>Birthday is empty<h2>";
            }
            if(password.isEmpty()) {
                msg += "<h2>Password is empty<h2>";
            }
            if(!password.equals(confirmation_pwd)) {
                msg += "<h2>Password is not mapping<h2>";
            }
            if(address.isEmpty()) {
                msg += "<h2>Address is empty<h2>";
            }
            
            if(msg.isEmpty()) {
        %>
        <jsp:useBean id="userBean" class="modals.User" scope="application"/>
        <jsp:setProperty name="userBean" property="name" value="<%=name%>"/>
        <jsp:setProperty name="userBean" property="address" value="<%=address%>"/>
        <jsp:setProperty name="userBean" property="birthday" value="<%=birthday%>"/>
        
        <jsp:forward page="welcome.jsp"/>
        <%
            } else {
                application.setAttribute("msg", msg);
        %>
        <jsp:forward page="error.jsp">
            <jsp:param name="msg_param" value="<%=msg%>"/>
            <jsp:param name="name_param" value="<%=name%>"/>
        </jsp:forward>
        <%    
            }
        %>
        <jsp:include page="footer.jsp"/>
    </body>
</html>


#register.jsp


<%-- 
    Document   : register
    Created on : Aug 5, 2020, 8:10:45 PM
    Author     : Diep
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<body>
        <jsp:include page="header.jsp"/>
	<div class="container">
		<div class="panel panel-primary">
			<div class="panel-heading">
				<h2 class="text-center">Registation Form - Input User's Detail Information</h2>
			</div>
			<div class="panel-body">
                            <form method="post" action="process.jsp">
				<div class="form-group">
				  <label for="usr">Name:</label>
                                  <input required="true" type="text" class="form-control" id="usr" name="name">
				</div>
				<div class="form-group">
				  <label for="email">Email:</label>
                                  <input required="true" type="email" class="form-control" id="email" name="email">
				</div>
				<div class="form-group">
				  <label for="birthday">Birthday:</label>
                                  <input type="date" class="form-control" id="birthday" name="birthday">
				</div>
				<div class="form-group">
				  <label for="pwd">Password:</label>
				  <input required="true" type="password" class="form-control" id="pwd" name="password">
				</div>
				<div class="form-group">
				  <label for="confirmation_pwd">Confirmation Password:</label>
                                  <input required="true" type="password" class="form-control" id="confirmation_pwd" name="confirmation_pwd">
				</div>
				<div class="form-group">
				  <label for="address">Address:</label>
                                  <input type="text" class="form-control" id="address" name="address">
				</div>
				<button class="btn btn-success">Register</button>
                            </form>
			</div>
		</div>
	</div>
        <jsp:include page="footer.jsp"/>
</body>
</html>


#welcome.jsp


<%-- 
    Document   : welcome.jsp
    Created on : Aug 12, 2020, 8:00:57 PM
    Author     : Diep
--%>

<%@page import="modals.User"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <%
            String name = request.getParameter("name");
            User user = (User) application.getAttribute("user");
            User userBean = (User) application.getAttribute("userBean");
        %>
        <h1>Welcome <%=name%>!</h1>
        <h1>Welcome <%=user.getName()%>!</h1>
        <h1>Welcome <%=userBean.getName()%>!</h1>
        <jsp:getProperty name="userBean" property="name"/>
    </body>
</html>


Tags:

Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)