By GokiSoft.com| 10:50 14/11/2020|
Web Component Development (SERVJSP)

[Share Code] Tìm hiểu JSP Servlet lớp J2010G - Lập trình JSP Servlet

#web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <servlet>
        <servlet-name>TestServlet</servlet-name>
        <servlet-class>servlet.TestServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>TestServlet</servlet-name>
        <url-pattern>/register</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>


#fail.jsp


<%-- 
    Document   : fail.jsp
    Created on : Nov 14, 2020, 8:52:42 AM
    Author     : teacher
--%>

<%@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>
        <h1>Failed!</h1>
    </body>
</html>


#success.jsp


<%-- 
    Document   : success
    Created on : Nov 14, 2020, 8:43:59 AM
    Author     : teacher
--%>

<%@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>
        <h1>POST DATA!</h1>
    </body>
</html>


#test.jsp


<%-- 
    Document   : test
    Created on : Nov 14, 2020, 8:35:57 AM
    Author     : teacher
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
	<title>JSP/Servlet Tutorial - Form Register</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>
    <!-- Code java trong JSP -->
        <%
            //code java o day
            String name = "";
            if(request.getParameter("name") != null) {
                name = request.getParameter("name");
            }
            
            String email = request.getParameter("email");
            if(email == null) {
                email = "";
            }
            
            String birthday = request.getParameter("birthday");
            if(birthday == null) {
                birthday = "";
            }
            
            String address = request.getParameter("address");
            if(address == null) {
                address = "";
            }
            
            String msg = "";
            String pwdMsg = "";
            if(!name.isEmpty()) {
                msg = "Dang ky tai khoan error!!!";
                pwdMsg = "Mat khau khong khop!!!";
            }
//            out.print(name);
        %>
	<div class="container">
		<div class="panel panel-primary">
			<div class="panel-heading">
				<h2 class="text-center">Registation Form - Input User's Detail Information</h2>
                                <h3 style="color: red"><%=msg%></h3>
			</div>
			<div class="panel-body">
                            <form method="post">
				<div class="form-group">
				  <label for="usr">Name:</label>
                                  <input required="true" type="text" class="form-control" id="usr" name="name" value="<%=name%>"/>
				</div>
				<div class="form-group">
				  <label for="email">Email:</label>
                                  <input required="true" type="email" class="form-control" id="email" name="email" value="<%=email%>"/>
				</div>
				<div class="form-group">
				  <label for="birthday">Birthday:</label>
                                  <input type="date" class="form-control" id="birthday" name="birthday" value="<%=birthday%>"/>
				</div>
				<div class="form-group">
                                    <label for="pwd">Password: <label style="color: red"><%=pwdMsg%></label></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="confirm_pwd"/>
				</div>
				<div class="form-group">
				  <label for="address">Address:</label>
                                  <input type="text" class="form-control" id="address" name="address" value="<%=address%>"/>
				</div>
                                <button class="btn btn-success" type="submit">Register</button>
                            </form>
			</div>
		</div>
	</div>
</body>
</html>


#welcome.jsp


<%-- 
    Document   : welcome
    Created on : Nov 14, 2020, 10:28:10 AM
    Author     : teacher
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Welcome Page</title>
    </head>
    <body>
        <!-- Code java trong JSP -->
        <%
            //code java o day
            String msg = (String) request.getAttribute("message");
            
            String name = request.getParameter("name");
            String email = request.getParameter("email");
            String birthday = request.getParameter("birthday");
            String address = request.getParameter("address");
            
//            out.print(name);
        %>
        <h1><%=msg%></h1>
        <h1>Welcome <%=name%>!</h1>
        <ul>
            <li>Name: <% out.print(name); %></li>
            <li>Email: <%=email%></li>
            <li>Birthday: <%=birthday%></li>
            <li>Address: <%=address%></li>
        </ul>
    </body>
</html>


#TestServlet.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 servlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author teacher
 */
public class TestServlet extends HttpServlet {

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/test.jsp");
        dispatcher.forward(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String name = request.getParameter("name");
        String email = request.getParameter("email");
        String birthday = request.getParameter("birthday");
        String password = request.getParameter("password");
        String confirmPwd = request.getParameter("confirm_pwd");
        String address = request.getParameter("address");
        
        //day vao CSDL nhu the nao.
        System.out.println("address: " + address);
        
        if(email.isEmpty() || !password.equals(confirmPwd)) {
            RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/test.jsp");
            dispatcher.forward(request, response);
            return;
        }
        //POST
        String msg = "Dang ky tai khoan thanh cong!!!";
        request.setAttribute("message", msg);
        
        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/welcome.jsp");
        dispatcher.forward(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}


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

5

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