【发布时间】:2015-01-08 17:46:35
【问题描述】:
我正在尝试从 JSP 链接到 servlet 。单击带有 name="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>
请帮我 。任何帮助将不胜感激。谢谢你。
【问题讨论】:
-
是的..@informatik 谢谢...我尝试使用按钮的onclick事件重定向...但只能检索jsp页面而不是servlet
-
似乎您正在尝试以 PHP 的方式进行操作:混合演示文稿和代码。您的 servlet 代码不应包含在 JSP 文件中。在 JSP 文件中使用 scriptlet 的还有highly discouraged。您要实现的目标似乎非常简单,我怀疑您只是在 Servlets/JSP 世界中不是很有经验。阅读有关 Servlets 的 Stack Overflow 信息页面,并查看简单的 servlet 使用示例。
-
这里也是最受欢迎的教程之一:Beginning & Intermediate Servlet & JSP Tutorials。希望这些对您有所帮助,因为无论如何您必须自己学习这些简单的东西。不要害怕——没那么可怕))
-
感谢 @informatik 提供的教程是 jsp 的新手,servlet 和 webconcepts 等在这方面没有太多经验。本教程有助于清除概念。再次感谢您
标签: java javascript jsp servlets