javascript
JSP怎么将表单提交到对应的servlet
昨天學習了這些內容,今天做一下分享吧,個人感覺挺亂的。。。。呵呵,其實沒事,慢慢就好了。難的不會,會的不難嘛!努力+認真就可以了,相信大家都可以的!加油!!!
下面的圖是我用myeclipse建立的項目的目錄。
目錄結構有點多哈。。。沒事其實很簡單的!
下面是我的index.jsp中的全部代碼:這里只是有一個表單,提交到TestServlet.java這個servlet中
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <%@ page language="java" contentType="text/html; charset=GB18030" ????pageEncoding="GB18030"%> <meta http-equiv="Content-Type" content="text/html; charset=GB18030"> <title>Insert title here</title> ????<h1>測試servlet</h1> ????? ????<hr> ????????<form action="servlet/TestServlet" method="post" dir="ltr"> ????????????<input name="userID" type="text"><br> ????????????<input value="查詢" type="submit"><br> ????????</form> ????????? ????b.jsp ???????? |
下面是TestServlet.java中的全部代碼:這個是獲取表單提交的內容,然后轉發到另一個頁面(c.jsp),其中這里包含一個業務邏輯(UserManager.java)
UserManager usermanager=new UserManager();
List userlist=usermanager.findUserByName(username);
以上這兩行代碼是業務邏輯的代碼,獲取數據。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | package com.majianjie.servlet; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class TestServlet extends HttpServlet { ????@Override ????protected void service(HttpServletRequest req, HttpServletResponse res) ????????????throws ServletException, IOException { ????????? ????????//取得表單數據 ????????? ????????String username=req.getParameter("username"); ????????UserManager usermanager=new UserManager(); ????????List userlist=usermanager.findUserByName(username); ????????? ????????req.setAttribute("userlist", userlist);//設置到req中,key-->value ????????? ????????//轉向到一個界面c.jsp,取出list中的數據 ????????req.getRequestDispatcher("/a/b/http://blog.csdn.net/majianjie/article/details/c/c.jsp").forward(req, res);//轉發getRequestDispatcher ????????? ????}?? } |
?
下面是UserManager.java 的代碼行(即業務邏輯)
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | package com.majianjie.servlet; import java.util.ArrayList; import java.util.List; public class UserManager { ????public List<string> findUserByName(String name){ ????????List<string> userList=new ArrayList<string>(); ????????userList.add("數據一"); ????????userList.add("數據二"); ????????userList.add("數據三"); ????????return userList; ????} } </string></string></string> |
下面是c.jsp的代碼
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <%@ page language="java" contentType="text/html; charset=GB18030" ????pageEncoding="GB18030"%> ????? <%@ page import="java.util.*" %> <meta http-equiv="Content-Type" content="text/html; charset=GB18030"> <title>Insert title here</title> c.jsp ????<p> ????<% ????????List userList = (List)request.getAttribute("userlist"); ????????out.println(userList); ????? ????%> ????</p><p> a.jsp </p> |
下面是a.jsp的代碼
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
a.jsp
下面是b.jsp的代碼
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
b.jsp
a.jsp
c.jsp
d.jsp
e.jsp
d.jsp, e.jsp中的代碼和a.jsp中的代碼一樣,唯一不同的就是: 將a改為d ,將a改為e......大部分代碼是自動生成的,很省事的。
最后是web.xml中的代碼:這里是一些配置信息。很重要的!!!嘿嘿嘿
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <!--?xml version="1.0" encoding="UTF-8"?--> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/javaee ????http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> ??<welcome-file-list> ????<welcome-file>index.jsp</welcome-file> ??</welcome-file-list> ??? ??<servlet> ????<servlet-name>TestServlet</servlet-name> ????<servlet-class>com.majianjie.servlet.TestServlet</servlet-class> ??</servlet> ??? ??<servlet-mapping> ????????<servlet-name>TestServlet</servlet-name> ????????<url-pattern>/servlet/TestServlet</url-pattern> ??</servlet-mapping> ??? </web-app> |
在表單中,當你點擊提交按鈕,觸發這個鏈接action="servlet/TestServlet"。請求到達服務器tomcat后,tomcat檢查當前項目的web.xml中所有的
配置中的的屬性值。看當中有沒有一個和請求地址servlet/TestServlet相同。結果找到如,如下:
TestServlet
/servlet/TestServlet
其中 /servlet/TestServlet 是你表單的訪問地址
然后,tomcat根據上面的TestServlet找到和
相對應的配置如下
TestServlet
com.majianjie.TestServlet
然后根據當中 找到TestServlet.java文件的地址。
然后通過設置tomcat就可以在瀏覽器中訪問了。例如我的配置好tomcat后在瀏覽器中輸入:http://localhost:8888/test_servlet/就可以訪問了,
結果如圖:
點擊查詢后:
點擊超級鏈接b.jsp后是:<喎�"http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PGltZyBzcmM9"http://www.2cto.com/uploadfile/Collfiles/20140225/20140225085659468.jpg" alt="">
總結
以上是生活随笔為你收集整理的JSP怎么将表单提交到对应的servlet的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JSP+Servlet + JDBC 实
- 下一篇: Server Tomcat v6.0 S