您將在下面的方式來使用的一種形式:
(我假設我們是從頭開始)
A.定義你的servlet在web.xml文件WEB-INF下:
<servlet>
<servlet-name>Login</servlet-name>
<servlet-class>cs336.servlets.LoginServlet</servlet-class>
<servlet>
<servlet-mapping>
<servlet-name>Login</servlet-name>
<url-pattern>/Login</url-pattern>
</servlet-mapping>
B.在你的.jsp頁面中,你需要以某種方式指向這個servlet的行動,你這樣做以下列方式:
<form class="LoginHeader" method="POST" action="Login">
<span>
<input type="text" name="user_name" placeholder="User Name/Email" required>
<input type="password" name="password" placeholder="Password" required>
<br>
<span id="loginError"></span>
<input type="submit" value="Log in" class="buttonWhite" id="loginButton"> <br>
<input type="submit" id="forgotPassword" value="I forgot my password...">
</span>
<a id="createAccount" href="index.html">Register</a>
</form>
C.在你的servlet中,你需要用適當的方法處理這個事件,在這種情況下:
protected void doPost( HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException { ... }
這既是我可以告訴你,你給了小背景。好看多了,你必須做一個POST調用servlet,和servlet方法攔截它,你必須做的事,然後你可以:
response.sendRedirect(...)
我希望這有助於。
來源
2014-08-27 20:13:58
Fer
你可以提供一些關於你的項目和一些片段的背景嗎? – 2014-08-27 19:41:29
好吧,onClick的工作原理是因爲您的瀏覽器帶有嵌入式JavaScript引擎。 Java是一個完全不同的故事。你希望它在客戶端還是在服務器端工作? – Leo 2014-08-27 19:48:07
@Leo:它應該在服務器端工作。 – Benben 2014-08-27 19:57:55