servlet開發簡單Java Web項目

一、表單
html

這裏form中action不能帶"/",不帶/表示相對路徑,帶「/」表示絕對路徑,必須寫成/項目名稱/url。java


<%@ 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>用戶登陸</title>
</head>
<body>
    <div>
	    <form action="loginServlet" method="post">
	        <table>
	            <tr>
	                <td>用戶名:</td>
	                <td><input id="userCode" name="userCode" type="text"></td>
	            </tr>
	            <tr>
	                <td>密碼:</td>
	                <td><input type="password" name="password"></td>
	            </tr>
	            <tr>
	                <td><input type="submit" value="登陸"></td>
	            </tr>
	        </table>
	    </form>
    </div>
</body>
</html>

二、web.xml配置web

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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">
  <display-name>pro-web02</display-name>
  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <servlet>
    <servlet-name>loginServlet</servlet-name>
    <servlet-class>cn.com.login.web.LoginController</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>loginServlet</servlet-name>
    <url-pattern>/loginServlet</url-pattern>
</servlet-mapping>

</web-app>

這裏的url-pattern中必須帶/,否則會報錯java.lang.IllegalArgumentException: Invalid <url-pattern> in servlet mappingapp


三、LoginController重寫doPost和doGet代碼jsp

參考w3cSchool中servlet教程 ide

http://www.runoob.com/servlet/servlet-form-data.html post

public class LoginController extends HttpServlet{
	private static final long serialVersionUID = 1L;

	public LoginController() {
		super();
	}
	
	public void doGet(HttpServletRequest request,
            HttpServletResponse response)
    throws ServletException, IOException
	{
	// 設置響應內容類型
	response.setContentType("text/html;charset=UTF-8");
	System.out.println("用戶名" + request.getParameter("userCode"));
	
	PrintWriter out = response.getWriter();
	String title = "Using GET Method to Read Form Data";
	String docType =
	"<!doctype html public \"-//w3c//dtd html 4.0 " +
	"transitional//en\">\n";
	out.println(docType +
	        "<html>\n" +
	        "<head><title>" + title + "</title></head>\n" +
	        "<body bgcolor=\"#f0f0f0\">\n" +
	        "<h1 align=\"center\">" + title + "</h1>\n" +
	        "<ul>\n" +
	        "  <li><b>名字</b>:"
	        + request.getParameter("userCode") + "\n" +
	        "</ul>\n" +
	        "</body></html>");
	}
// 處理 POST 方法請求的方法
public void doPost(HttpServletRequest request,
             HttpServletResponse response)
	throws ServletException, IOException {
	doGet(request, response);
}
}
相關文章
相關標籤/搜索