Eclipse creates a new maven project based on Servlet3.x

foreword

I haven't played servlets for a long time, and I still struggled with java when I was in school ten years ago. Recently, I wanted to use Eclipse to create a new maven project based on Servlet3.x, but I didn't expect to toss for a while. Just write an article as an example for reference.

Environmental preparation

eclipse:Oxygen.1 Release (4.7.1)

servlet:3.1.0

Create a new maven project

 

 convert to serlvet3.0

 At this point we found that the Dynamic Web Module cannot be converted to 3.0, which is the most troublesome part of this project.

 We find the org.eclipse.wst.common.project.facet.core.xml file in the project.settings directory:

After opening, edit the version of jst.web to 3.0 and save it.

 Then re-maven update the project.

Then open it again and find that the version of Dynamic Web Module has changed to 3.0. The conversion has been successful at this point.

 Write relevant code

First we edit the pom.xml file

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.sadoshi</groupId>
	<artifactId>shiroWeb</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>shiroWeb Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<dependency>
			<groupId>commons-logging</groupId>
			<artifactId>commons-logging</artifactId>
			<version>1.1.3</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.1.0</version>
			<scope>provided</scope>
		</dependency>
	</dependencies>
	<build>
		<finalName>shiroWeb</finalName>
	</build>
</project>

Then edit the src/main/webapp/WEB-INF/web.xml file. If there is no webapp directory, you can create a new one according to this path.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" 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>20180510</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>HelloServlet</servlet-name>
    <servlet-class>com.sadoshi.shiroWeb.servlet.HelloServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>HelloServlet</servlet-name>
    <url-pattern>/hello</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>LoginServlet</servlet-name>
    <servlet-class>com.sadoshi.shiroWeb.servlet.LoginServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>LoginServlet</servlet-name>
    <url-pattern>/login</url-pattern>
  </servlet-mapping>
</web-app>

 Here we simply define the welcome page, which is the default root path access page. In addition, when accessing /hello, we will call HelloServlet, and when accessing /login, we will call LoginServlet.

Then write HelloServlet, which is the home page of the website. It's very simple, just visit this servlet and it will return the hello.jsp page:

package com.sadoshi.shiroWeb.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloServlet extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.getRequestDispatcher("/hello.jsp").forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
}

 Then write LoginServlet as login processing. It is also very simple here, if it is accessed through get, it will return to the login.jsp page. If it is accessed via post, set a token for it and redirect to the homepage. The link of login authentication is omitted here:

package com.sadoshi.shiroWeb.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginServlet extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.getRequestDispatcher("/login.jsp").forward(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		Cookie cookie = new Cookie("token","123");
		resp.addCookie(cookie);
		resp.sendRedirect("/shiroWeb/hello");
	}

}

 Note that the servlet-api version in the pom must be 3.x. If the latest 4.x is used, some implementations may be different, which will affect the running effect. For example, when I used 4.x before, the processing of the Filter link was somewhat different, so it is best to use the same version.

Now we still lack two pages, the first is the home page hello.jsp, we create it under the webapps directory:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Hello</title>
</head>
<body>
	<h1>Hello Page</h1>
</body>
</html>

Create a new login page login.jsp in the same directory

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户登录页面</title>
</head>
<body>
     <h2>${remind}</h2>
     <h1>登录页面</h1>
     
     <form action="login" method="post">
           登录名:<input type="text" name="name"><br/>
             密码为:<input type="password" name="password"><br/>
           <input type="submit" value="登录">
     </form>
</body>
</html>

 This simple servlet is done

test

We create a new server under the Servers of eclipse, if not, search for it in Window -> Show View -> Other...

 I have installed tomcat8.0 on this machine, just select the corresponding directory.

 Then right-click the newly created server and select Add and Remove, and add our project to the right:

 Right-click the server and select Start, then visit http://localhost:8080/shiroWeb/login to open the login page

We enter the login name and password casually, and normally it will jump to the /hello path:

And we click F12, and then refresh the page, we can see that token=123 is set on Cookies. This is similar to the system assigning a token after login. Later, the system can judge whether the user has logged in according to the token field.

summary

If you want to do your job well, you must first sharpen your tools. Creating a servlet project is the most basic content for learning JavaWeb. I hope readers can get the knowledge they want in the article.

Guess you like

Origin blog.csdn.net/sadoshi/article/details/120570168