标签:style blog class code java c
Servlet 是 Java 服务端程序,Servlet 程序是运行在服务器上的
Seervlet 程序代码:
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(); } }
web.xml 设置:
<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>
浏览器访问:
http://localhost:8080/JavaWeb/HelloWorld
即可
新建 JSP 文件 HelloWorld.jsp:
<%@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>
浏览器访问:
http://localhost:8080/JavaWeb/HelloWorld.jsp
即可
一个用户登录程序
LoginServlet:
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); } }
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=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>
web.xml 配置:
<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>
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
标签:style blog class code java c
原文地址:http://www.cnblogs.com/geb515/p/3726642.html