[英]Redirecting to a servlet from a JSP on button click
我正在嘗試從JSP鏈接到servlet。 在單擊帶有名稱=“ conf”的按鈕時,我需要重定向到servlet“ / Initial”。 問題是當我使用type="button"
什么也沒發生,而當我使用type="submit"
,頁面被定向到servlet“ / Initial”並在那里執行操作。 無法識別問題。
這是我的代碼:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ page import="reg.serv.*"%>
<!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 method="post">
<center>
<table border="1" width="30%" cellpadding="3">
<thead>
<tr>
<th colspan="2">Register Here</th>
</tr>
</thead>
<tbody>
<tr>
<td>Username</td>
<td><input type="text" class="" id="username" name="username1" value="" /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password1" id="password" value="" /></td>
</tr>
<tr>
<td>Confirm Password</td>
<td><input type="password" name="confirmpassword1" id="confirmpassword" value="" /></td>
</tr>
<tr>
<td>Mobile Number</td>
<td><input type="text" class="" id="mob" name="mob1" value="" /></td>
</tr>
<tr>
<td>Email ID</td>
<td><input type="text" class="" id="email" name="email1" value=" " /></td>
</tr>
<tr>
<td>Address</td>
<td><textarea id="address" name="address1"></textarea></td>
</tr>
<tr>
<td colspan="2">Already registered <a href="Index.jsp">Login Here</a></td>
</tr>
</tbody>
<tr>
<td><input type="button" value="confirm" name="conf" /></td>
<td><input type="reset" value="Reset" /></td>
<td><input type="button" value="Cancel" name="Cr" onclick="openPage('Initial.jsp')" /></td>
</tr>
</table>
</form>
<script type="text/javascript">
function openPage(pageURL) {
window.location = pageURL;
}
</script>
<%
String x = request.getParameter("conf");
if (x != null && x.equals("confirm")) {
//response.sendRedirect("/Initial");
RequestDispatcher dispatcher = request.getRequestDispatcher("/Initial");
dispatcher.forward(request, response);
}
%>
</body>
</html>
請幫我 。 任何幫助將不勝感激。 感謝您。
你必須寫
<form action=/your_servlet_page_name>
而且你必須使用
<input type="submit" value="confirm" name="conf"/>
而且,您還必須將servlet頁面映射到web.xml文件中,例如
<servlet-mapping>
<servlet-name>CheckLogin</servlet-name>
<url-pattern>/CheckLogin</url-pattern>
</servlet-mapping>
<form action = "servlet-name" method = "method in the servlet"> <input type ="submit" value = "val"> </form>
這是執行此操作的簡單方法。 如果您使用的是我認為7以上版本的最新jre,則無需在web.xml文件中聲明servlet。 @WebServlet(“ / servlet-url”)可以解決問題。
嘗試只更改腳本
<script type="text/javascript">
function openPage(pageURL)
{
window.location.href = pageURL;
}
</script>
function openPage(pageURL) {
window.location = pageURL;
}
在上面的代碼段中,pageURL必須是一個絕對URL,在處理servlet的情況下可能會有所不同,因此可以使用以下URL代替
location.href = (location.href).substr(0, (location.href).lastIndexOf('xyz.jsp'))+"/abc";
這里的``abc''是我們必須將``xyz.jsp''重定向到的servlet,即使有很多按鈕也可以使用,可以編寫相應的函數來重定向到相應的servlet。 在輸入類型為“按鈕”或“提交”的情況下也可以使用。
我沒有確切地知道您要做什么,但是重定向正在使用: response.sendRedirect(request.getContextPath());
要么
response.sendRedirect(String url);
如果要使用type =“ button”而不是type =“ submit”。 您可以在按鈕上使用javascript功能。 喜歡
<script>
function doSubmit(){
var actionURL ="MENTION URL YOU WANT TO REDIRECT";
// perform your operations
myForm.submit(actionURL); OR
myForm.submit();
}
</script>
<form name="myForm">
<input type="button" name="conf" value="conf" obclick="doSubmit();">
</form>
希望對您有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.