码迷,mamicode.com
首页 > 编程语言 > 详细

Java Web(2):第一个 Servlet 程序

时间:2014-05-15 15:59:55      阅读:446      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   java   c   

Servlet 是 Java 服务端程序,Servlet 程序是运行在服务器上的

bubuko.com,布布扣

 

Seervlet 程序代码:

bubuko.com,布布扣
package g.servlet;

import java.io.IOException;
import java.io.PrintWriter;

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

public class HelloWorldServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");

        PrintWriter out = response.getWriter();

        out.println("<html><head><title>Hello World</title></head>");

        out.println("<body><h1>hello, world</h1></body><html>");
        
        out.println("<br>" + new java.util.Date().toString());

        out.flush();

    }

}
bubuko.com,布布扣

 

web.xml 设置:

bubuko.com,布布扣
<servlet>
    <servlet-name>HelloWorld</servlet-name>
    <servlet-class>g.servlet.HelloWorldServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>HelloWorld</servlet-name>
    <url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>
bubuko.com,布布扣

 

浏览器访问:

http://localhost:8080/JavaWeb/HelloWorld

即可

 

新建 JSP 文件 HelloWorld.jsp:

bubuko.com,布布扣
<%@page import="java.util.Date"%>
<%@ 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>Insert title here</title>
</head>
<body>
    <h1>hello, world</h1>
    <%= new Date().toString() %>
</body>
</html>
bubuko.com,布布扣

 

浏览器访问:

http://localhost:8080/JavaWeb/HelloWorld.jsp

即可

 


 

 

一个用户登录程序

LoginServlet:

bubuko.com,布布扣
package g.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 LoginServlet extends HttpServlet {

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

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String username = request.getParameter("username");
        String password = request.getParameter("password");

        System.out.println(username + ":" + password);

    }
}
bubuko.com,布布扣

 

login.jsp:

bubuko.com,布布扣
<%@ 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>Login</title>
</head>
<body>
    <form action="LoginServlet" method="post">
        username:<input type="text" name="username">
        password:<input type="password" name="password">
        <input type="submit" value="submit">
        <input type="reset" value="reset">
    </form>

</body>
</html>
bubuko.com,布布扣

 

web.xml 配置:

bubuko.com,布布扣
<servlet>
    <servlet-name>LoginServlet</servlet-name>
    <servlet-class>g.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>LoginServlet</servlet-name>
    <url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
bubuko.com,布布扣

 

GET 和 POST 的区别:

get,默认方式:

GET /LoginServlet?username=user&password=pass HTTP/1.1

 

post:(文件上传必须用 post)

POST /LoginServlet HTTP/1.1

 

两者向服务器端发送请求时的形式是不同的

 

 

Java Web(2):第一个 Servlet 程序,布布扣,bubuko.com

Java Web(2):第一个 Servlet 程序

标签:style   blog   class   code   java   c   

原文地址:http://www.cnblogs.com/geb515/p/3726642.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!