• Thế Giới Giải Mã

    Bí ẩn nhân loại Leonardo Da Vinci

  • Thế Giới Giải Mã

    Anh hùng thầm lặng Nikola Tesla

  • Thế Giới Giải Mã

    Thần đèn Thomas Edison

  • Thế Giới Giải Mã

    Người thôi miên Adolf Hitler

Showing posts with label Jsp & Servlet. Show all posts
Showing posts with label Jsp & Servlet. Show all posts

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

HelloWorld Servlet JSP Basic + Eclipse

Cấu trúc project
Creating a Java Dynamic Web Project
Creating a Java Servlet
HelloWorld.java
Servlet JSP 2018
package com.giaima;

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;

/**
 * Servlet implementation class HelloWorld
 */
@WebServlet("/hello-world")
public class HelloWorld extends HttpServlet {
   private static final long serialVersionUID = 1 L;

   /**
    * @see HttpServlet#HttpServlet()
    */
   public HelloWorld() {
      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
      response.getWriter().append("Served at: ").append(request.getContextPath());

      String yourName = request.getParameter("yourName");
      PrintWriter writer = response.getWriter();
      writer.println("<h1>Hello " + yourName + "</h1>");
      writer.close();
   }

   /**
    * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    */
   protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      // TODO Auto-generated method stub
      doGet(request, response);
   }
}
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>
      <h1>Hello JSP and Servlet!</h1>
      <form action="hello-world" method="post">
         Enter your name: <input type="text" name="yourName" size="20">
         <input type="submit" value="Call Servlet" />
      </form>
   </body>
</html>
Result

27/10/2018

 

BACK TO TOP

Xuống cuối trang