触摸一下Servlet

一、安装Tomcat:

    下载免安装包,解压;

    设置环境变量:

    



配置CATALINA_HOME路径,然后把路径配置到Path环境变量中:%CATALINA_HOME%\bin


Tomcat配置完成之后,打开Eclipse,配置Tomcat


然后新建 Dynamic Web Project,在项目src下面新建一个.java文件 ,继承    javax.servlet.http.HttpServlet。

嗯 然后在下面的栏目工具栏 servers里面配置tomcat,跟上面一样。

到这里,基本上配置完成了,开始写servlet。




写完这些 还需要配置webcontent中WEB-INF的web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>testServlet</display-name>
  <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>
  
  	<!-- 声明servlet -->
  	<servlet>
  		<servlet-name>firstServlet</servlet-name>
  		<servlet-class>com.lantu.enbir.FirstServlet</servlet-class>
  	</servlet>
  	<!-- servlet与请求地址的映射 -->
  	<servlet-mapping>
  		<servlet-name>firstServlet</servlet-name>
  		<url-pattern>/firstServlet</url-pattern>
  	</servlet-mapping>
  	
</web-app>

主要是写这几句话,配置servlet

然后在下面的工具栏servers中,运行tomcat服务器,然后打开浏览器,输入localhost:8080/testServlet/firstServlet,就可以在控制台看到要输出的文字。(testServlet是项目名,firstServlet是映射中servlet的名称)

我们接下来写一下仿登录

在webcontent目录下新建一个login.jsp:

<%@ 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=ISO-8859-1">
<title>login 提交</title>
</head>
<body>
	<form action="firstServlet">
		姓名:<input name="username" type="text">
		密码:<input name="pwd" type="text">
		<input type="submit" value="login">
		
	</form>
</body>
</html>

这里包含了一个表单,action是firstServlet,包含username和pwd,然后包含提交submit,一点击,就进入了firstServlet。然后firstServlet对请求进行处理。

import java.io.IOException;
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 FirstServlet
 */
@WebServlet("/FirstServlet")
public class FirstServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FirstServlet() {
        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());
		doPost(request, response);
	}

	/**
	 * @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);
		System.out.println("servlet biu~");
		String username = request.getParameter("username");
		String pwd = request.getParameter("pwd");
		if(username.equals("admin")) {
			System.out.println("login successfully");
			request.setAttribute("username", username);
			request.getRequestDispatcher("success.jsp").forward(request, response);	//只是页面的跳转 在服务器端,将请求发送到另一个页面,对浏览器而言,只发送了一次请求
		}else {
			System.out.println("login failed");
			response.sendRedirect("loginfail.jsp");	//重定向  让浏览器再重新发一次请求,两次请求
		}
	}

}
request.getRequestDispatcher("success.jsp").forward(request, response);

这是跳转到success.jsp的方法

success.jsp

<%@ 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=ISO-8859-1">
<title>login 提交</title>
</head>
<body>


	<h1>${username} login success!!!</h1>
	
</body>
</html>

else跳转到loginfail.jsp

<%@ 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=ISO-8859-1">
<title>login 提交</title>
</head>
<body>
	<h1>login failed!!!</h1>
</body>
</html>

到这里,基本上就走完了一套流程。

这是自己做笔记用的,重点在于符合自己的习惯,自己看得懂就好得意


猜你喜欢

转载自blog.csdn.net/enbir/article/details/80915658