servlet入門(1)

4957 ワード

私はeclipse+tomcat+mysqlの開発環境1を使って、新しいwebプロジェクトを作って、過程の中で決してwebを作成することを忘れないでください.xmlファイル2、java Resourcesでservlet 3を作成し、webcontentフォルダでjspページを作成します.次は私の最初のjspページの情報です.
"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情報の追加と情報の構成
あとで運転すればいいです