27 October 2018

Hello Servlet JSP Basic + Web.xml + Post +Get + Eclipse

Cấu trúc project
Creating a Java Dynamic Web Project
Creating a Java Servlet

HelloServlet.java
Servlet JSP 2018
package com.giaima;

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 HelloServlet
 */
@WebServlet("/helloServlet")
public class HelloServlet extends HttpServlet {
   private static final long serialVersionUID = 1 L;

   /**
    * @see HttpServlet#HttpServlet()
    */
   public HelloServlet() {
      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
      String name = request.getParameter("name");
      if (name != "" || name != null) {
         request.setAttribute("nameAtr", name);
         RequestDispatcher rd = request.getRequestDispatcher("home.jsp");
         rd.forward(request, response);
         /*response.sendRedirect("home.jsp");*/
      } else {
         response.sendRedirect("index.jsp");
      }
   }

   /**
    * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    */
   protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      // TODO Auto-generated method stub
      String name = request.getParameter("name");
      if (name != "" || name != null) {
         request.setAttribute("nameAtr", name);
         RequestDispatcher rd = request.getRequestDispatcher("home.jsp");
         rd.forward(request, response);
         /*response.sendRedirect("home.jsp");*/
      } else {
         response.sendRedirect("index.jsp");
      }
   }
}
Creating a JSP Page
index.jsp
Servlet JSP 2018
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
   pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
   <head>
      <meta charset="ISO-8859-1">
      <title>Insert title here</title>
   </head>
   <body>
      <form action="helloServlet" method="GET">
         Name: <input type="text" name="name"/>
         <button>GET</button>
      </form>
      <form action="helloServlet" method="POST">
         Name: <input type="text" name="name"/>
         <button>POST</button>
      </form>
   </body>
</html>
home.jsp
Servlet JSP 2018
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
   pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
   <head>
      <meta charset="ISO-8859-1">
      <title>Insert title here</title>
   </head>
   <body>
      <h1> Home Page</h1>
      <p>getParameter name: <%=request.getParameter("name") %></p>
      <p>getAttribute name: <%=request.getAttribute("nameAtr") %></p>
   </body>
</html>
Add config servlet mapping
web.xml
Servlet JSP 2018
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
   <display-name>servlet-jsp-helloworld-xml</display-name>
   <servlet>
      <servlet-name>helloServlet</servlet-name>
      <servlet-class>com.giaima.HelloServlet</servlet-class>
   </servlet>
   <servlet-mapping>
      <servlet-name>helloServlet</servlet-name>
      <url-pattern>/helloSevlet</url-pattern>
   </servlet-mapping>
   <welcome-file-list>
      <welcome-file>index.html</welcome-file>
      <welcome-file>index.htm</welcome-file>
      <welcome-file>index.jsp</welcome-file>
      <welcome-file>default.html</welcome-file>
      <welcome-file>default.htm</welcome-file>
      <welcome-file>default.jsp</welcome-file>
   </welcome-file-list>
</web-app>
Result
27/10/2018

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang