servlet入门(1)

我使用的是eclipse+tomcat+mysql的开发环境
1,新建一web项目,过程中切勿忘记点选创建web.xml文件
2,在java Resources中创建servlet
3,在webcontent文件夹中创建一个jsp页面,下面是我的第一个jsp页面的信息

<%@ page language="java" contentType="text/html; charset=uf-8"
    pageEncoding="ISO-8859-1"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title heretitle>
head>
<body>
    <h1>this is my first servlet programeh1>
    <hr>
    <a href="servlet/HelloServlet">处理get请求a>
body>
html>

4,之后开始写sevlet的程序(重写doget()方法)

package servlet;

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

    /**
     * @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
        response.getWriter().append("Served at: ").append(request.getContextPath());
        System.out.println("处理get请求");
        PrintWriter out = response.getWriter();
        out.print("hello servlet
"
); } /** * @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); } }

5,在web.xml中配置sevlet信息
增加和信息

之后运行就可以了

你可能感兴趣的:(java-web)