Servlet Getting Case (a) recognize Servlet

1. What is Servlet?
Servlet is a small program that runs on a server, a servlet is a Java class, and can use the "request - response" to access the servlet program which resides in the server memory programming model

2. Why use servlet?

After we put into the html tomcat, these pages can be accessed through a browser. But these pages are static and do not change the content. Anyone, any time to see the content is the same.
Servlet is possible to generate dynamic html source. Servlet is a java class, he was running on the server.

3, the first case of Servlet

  • Write a java class that inherits HTTPServlet, it is a Servlet class . When you create can also be directly specify the parent class
  • Override doGet () or doPost () method, or a method override service , right -> source-> override
  • Servlet arranged in web.xml access path and the fully qualified name

Note: When using myeclipse2014 time and later, the new web project does not web.xml file,
resolved: after new projects, filling out the project name, do not point finish points next, and then select the Create web.xml file
using myeclipse write servlet
src-> new-> servlet
override the doGet () doPost ()
deployment run

For example: the current time dynamic display of the Servlet to the browser:
writing the steps of:

①. Write the servlet class, Servlet class should inherit HttpServlet

package com.macw.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * servlet类要继承HttpServlet
 * HttpServlet java项目中没有这个类,需要导入外部的jar包。
 * jar包在tomcat安装路径的lib文件夹里面。
 * @author Administrator
 * 重写父类中的servcie方法。
 */
public class FirstServlet extends HttpServlet {
	public void service(HttpServletRequest request,HttpServletResponse response) throws IOException{
		//生成动态的html源码
		Date d=new Date();
		System.out.println(d);
		String html="<html>"
				+ "<head><title>我是标题</title></head>"
				+"<body>"+d+"</body>"
				+ "</html>";
		//把html源码响应给浏览器
		//设置服务器给浏览器响应的数据的格式是html
		response.setContentType("test/html");
		//设置服务器给浏览器响应的数据的编码集
		response.setCharacterEncoding("utf-8");
		//设置具体的响应内容。
		PrintWriter out=response.getWriter();
		out.print(html);
		out.close();
	}
}

②. Servlet class compiled into class files
MyEclipse will automatically compile the java file.
③. The tomcat into the class file to the appropriate directory (deploy servlet)

Tomcat
 |-webapps
    |-myweb
        |-WEB-INF
            |-classes(专门存放servlet类)
                |-com.macw.servlet.FirstServlet(class文件)
            |-web.xml

④. Each servlet need to be configured in the configuration file.

<?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">
	<servlet>
	<!--servlet的名字,是随便起的,要跟servlet-mapping里面的servlet-name一致,一般默认和servlet类名相同 -->
		<servlet-name>FirstServlet</servlet-name>
		<!--servlet的包名和类名(全限定名) -->
		<servlet-class>com.macw.servlet.FirstServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<!--要跟servlet中的servlet-name保持一致 -->
		<servlet-name>FirstServlet</servlet-name>
		<!--url-patten要以/开头,后面内容随便写,是浏览器的请求路径 -->
		<url-pattern>/first</url-pattern>
	</servlet-mapping>

4, common errors

①.404 error.
Root Cause: resource access does not exist.
A. Check your path access, that is, the browser URL input. Url request.
B. Check whether tomcat start time error. And then locate the error according to the error message tomcat.

②405 error
405 Error
error because the servlet's service method class is not rewritten.

③500 error
500 error
number 500 for the wrong reasons, simply because the recovery of their abnormal java class.
After 500 error to see the detailed error information. And then locate the error in accordance with error message.

④. Address is occupied
Tomcat port number is occupied
port number Tomcat is occupied.

Guess you like

Origin blog.csdn.net/MacWx/article/details/92378965