標籤:串連資料庫 初學者
1、首先說明一點,雖然很簡單但是對於初學者 這個jsp和servlet的連結 我搗鼓了半天才算弄明白
jsp將請求提交到一個url,然後servlet擷取也從這個url中擷取請求的資料,兩者的連結就在那個url
jsp中的代碼
檔案名稱:login.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>登入</title>
</head>
<body>
<form action="http://localhost:80/loginServlet/LoginServlet" method="post">
使用者:<input type="text" name="username" value="wangmin"/><br/>
密碼:<input type="password" name="password" value="wangmin"/><br/>
<input type="submit" value="登入" />
</form> aaaa
</body>
</html>
servlet中的代碼
檔案名稱:LoginServlet.java
package my.experience.login;
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{
//重寫doGet方法
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException,
IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
//伺服器端列印資訊
System.out.println("username=" + username);
System.out.println("password=" + password);
//設定編碼格式
response.setContentType("text/html;charset=GB18030");
//返回html頁面
response.getWriter().println("<html>");
response.getWriter().println("<head>");
response.getWriter().println("<title>登入資訊</title>");
response.getWriter().println("</head>");
response.getWriter().println("<body>");
response.getWriter().println("歡迎【" + username + "】使用者登入成功!!!");
response.getWriter().println("</body>");
response.getWriter().println("</html>");
}
//重寫doPost方法
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException,
IOException {
doGet(request, response);
}
}
在web.xml中一定要寫對 url,要不然找不到
<?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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>MyExperience</display-name>
<welcome-file-list>
<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>my.experience.login.LoginServlet</servlet-class>//包名+類名
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/loginServlet/LoginServlet</url-pattern> 對應的jsp中的action
</servlet-mapping>
</web-app>
然後啟Tomacat,在瀏覽器中輸入http://localhost:80/login.jsp就ok
本文出自 “autoComplete” 部落格,請務必保留此出處http://7129486.blog.51cto.com/7119486/1595806
jsp和servlet 簡單登入介面(不串連資料庫)