2013-10-16 85 views
3

我是Servlet功能的新手。我試圖在JSP窗體中獲取一些數據,並試圖使用Servlet將其打印在控制檯中。但我無法做到這一點。如何從JSP頁面獲取數據到servlet

的web.xml

<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"> 

    <servlet> 
    <servlet-name>controlServlet</servlet-name> 
    <servlet-class>com.selenium8x8.servlet.ControlServlet</servlet-class> 
    </servlet> 

    <servlet-mapping> 
    <servlet-name>controlServlet</servlet-name> 
    <url-pattern>/*</url-pattern> 
    </servlet-mapping> 
</web-app> 

Startup.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<!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=ISO-8859-1"> 
<title>Insert title here</title> 
</head> 
<body> 
<form action="Startup" method="post"> 
     <input type="text" name="name"/><br>   
     <input type="text" name="group"/> 
     <input type="text" name="pass"/> 
     <input type="submit" value="submit">    
    </form> 

</body> 
</html> 

ControlServlet.java

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 ControlServlet extends HttpServlet { 

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     String name = request.getParameter("name"); 
     String group = request.getParameter("group"); 
     String pass = request.getParameter("pass"); 
     System.out.println("Name :"+ name); 
     System.out.println("group :"+ group); 
     System.out.println("pass :"+ pass); 
    } 

} 

EXECUT後離子,它拋出我下面的錯誤,

HTTP Status 405 - HTTP method GET is not supported by this URL 

type Status report 

message HTTP method GET is not supported by this URL 

description The specified HTTP method is not allowed for the requested resource. 
+0

檢查您的URL。請求發送到什麼地址? –

+0

'

+0

什麼問題passig數據從JSP到Servlet或頁面不顯示? –

回答

2

@Prassana:請如下修改web.xml和它應該工作。我測試了你的代碼併爲我工作。這將適用於GET和POST。

<servlet> 
<servlet-name>ControlServlet</servlet-name> 
<servlet-class>com.selenium8x8.servlet.ControlServlet</servlet-class> 
    </servlet> 

    <servlet-mapping> 
<servlet-name>ControlServlet</servlet-name> 
<url-pattern>/Startup</url-pattern> 
    </servlet-mapping> 
</web-app> 
+0

但我得到HTTP狀態404 -/Testing_automation/ 類型狀態報告 消息/ Testing_automation/ description請求的資源不可用。 – Prasanna

+0

@Prasanna:你可以給你正在嘗試的完整url嗎? – user2821894

+0

哪一個..?我不能得到你 – Prasanna

0

需要具有以下

<form action="/Startup" method="post"> 
+0

我現在得到這個錯誤,HTTP方法GET不支持這個URL – Prasanna

+0

更改方法doGet(...)與doPost(...)它應該工作 –

0

更換 這個改變表單標籤的行動:通過本<form action="Startup"

<form action="/Startup"

1

變化映射

<form action="/Startup" method="post"> 

第二步:添加註釋ovveride

@Override 
    public void doPost(HttpServletReques... 

它是無法檢測到您的文章的方法和試圖擊中get方法我猜。

,並嘗試與get方法檢查也一度,

@Override 
    public void doGet(HttpServletReques... 
+0

但我仍然得到錯誤先生。我已更新問題 – Prasanna

+0

@Prasanna這不應該發生。你可以像'method =「GET」'一樣嘗試並在doGet方法中粘貼相同的代碼並檢查? –

+0

同樣的事情發生.. – Prasanna

相關問題