我有一个这样的jsp,我想处理一些输入数据。为什么当前的jsp不等待提交值然后执行servlet?该站点立即重定向到定义的下一页。我想显示页面,用户应该输入一个值,然后重定向。
这是我的代码:
<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<%@ include file="/resources/taglibs.jsp"%>
<%
session.setAttribute("MyName", System.getProperty("user.name"));
request.setAttribute("hideNav", true);
%>
<fmt:setLocale value="${lang}" />
<fmt:bundle basename="messages">
<t:template>
<jsp:attribute name="header">
<fmt:message key="topic.bag_transfer" />
</jsp:attribute>
<jsp:attribute name="navigation_static">
<li><a href="info"><font size="+0.1">${lang}</font></a></li>
</jsp:attribute>
<jsp:body>
<p class="msg">
<fmt:message key="all.message" />
<c:out value="${requestScope.Message}" />
</p>
<%--Form--%>
<form method="post" action="login">
<div>
<p class="f_head">
<fmt:message key="all.user" />
</p>
<p class="f_form">
<input id="1" class="f_txt" type="text" name="login" />
</p>
<%--button--%>
<p class="f_form_bl">
<input id="3" class="f_btn" type="submit" name="btn" value="Weiter" />
</p>
</div>
</form>
</jsp:body>
</t:template>
和像这样的servlet
public class BagScanStartLocation extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String CONTENT_TYPE = "text/html; charset=UTF-8"; //$NON-NLS-1$
private static final String PAGE_JSP_THIS = "/bag_scan_start_location.jsp"; //$NON-NLS-1$
private static final String PAGE_JSP_LOGIN = "/"; //$NON-NLS-1$
private static final String PAGE_JSP_NEXT = "/scan_bag.jsp"; //$NON-NLS-1$
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
response.setCharacterEncoding("UTF-8"); //$NON-NLS-1$
HttpSession session = request.getSession(true);
Locale lang = (Locale) session.getAttribute("lang"); //$NON-NLS-1$
AppsSession myAppsUser = (AppsSession) session.getAttribute("USER"); //$NON-NLS-1$
//.....
request.setAttribute("UserName", myAppsUser.getUserName()); //$NON-NLS-1$
request.setAttribute("messageText", "Test"); //$NON-NLS-1$
RequestDispatcher disp = this.getServletContext().getRequestDispatcher("/scan_bag.jsp");
disp.forward(request, response);
}
我尝试了一个简单的例子:
的servlet
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
// reading the user input
String color= request.getParameter("color");
PrintWriter out = response.getWriter();
out.println (
"<html> \n" +
"<head> \n" +
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\"> \n" +
"<title> My first jsp </title> \n" +
"</head> \n" +
"<body> \n" +
"<font size=\"12px\" color=\"" + color + "\">" +
"Hello World" +
"</font> \n" +
"</body> \n" +
"</html>"
);
JSP
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title> My first JSP </title>
</head>
<body>
<form action="BagScanStartLocation">
Please enter a color <br>
<input type="text" name="color"size="20px">
<input type="submit" value="submit">
</form>
</body>
我将它链接到topic.jsp中,就像这样
<li><a id="10" href="bag_scan_start_location"> <fmt:message key="topic.bag_transfer" /></a></li>
和web.xml:
<servlet-mapping>
<servlet-name>BagScanStartLocation</servlet-name>
<url-pattern>/bag_scan_start_location</url-pattern>
</servlet-mapping>
<servlet>
<description>
</description>
<display-name>BagScanStartLocation</display-name>
<servlet-name>BagScanStartLocation</servlet-name>
<servlet-class>de.blablabla.oracle.blab.BagScanStartLocation</servlet-class>
</servlet>
当我点击主题href时,servlet将立即执行。为什么JSP 未显示?
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>bla_WA</display-name>
<welcome-file-list>
<welcome-file>index</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Root</servlet-name>
<servlet-class>de.xxx.oracle.bla.Root</servlet-class>
</servlet>
<servlet>
<servlet-name>Login</servlet-name>
<servlet-class>de.xxx.oracle.xxsh.Login</servlet-class>
</servlet>
<servlet>
<servlet-name>Info</servlet-name>
<servlet-class>de.xxx.oracle.xxsh.InfoServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>testreplier</servlet-name>
<servlet-class>de.xxx.oracle.xxsh.testreplier</servlet-class>
</servlet>
<servlet>
<servlet-name>CycleCountIndex</servlet-name>
<servlet-class>de.xxx.oracle.xxsh.CycleCountIndex</servlet-class>
</servlet>
<servlet>
<servlet-name>CycleCountLocator</servlet-name>
<servlet-class>de.xxx.oracle.xxsh.CycleCountLocator</servlet-class>
</servlet>
<servlet>
<servlet-name>CycleCountAddItem</servlet-name>
<servlet-class>de.xxx.oracle.xxsh.CycleCountAddItem</servlet-class>
</servlet>
<servlet>
<servlet-name>SimpleCycleCountIndex</servlet-name>
<servlet-class>de.xxx.oracle.xxsh.SimpleCycleCountIndex</servlet-class>
</servlet>
<servlet>
<servlet-name>SimpleCycleCountLocator</servlet-name>
<servlet-class>de.xxx.oracle.xxsh.SimpleCycleCountLocator</servlet-class>
</servlet>
<servlet>
<servlet-name>SimpleCycleCountAddItem</servlet-name>
<servlet-class>de.xxx.oracle.xxsh.SimpleCycleCountAddItem</servlet-class>
</servlet>
<servlet>
<servlet-name>ReturnMtxIndex</servlet-name>
<servlet-class>de.xxx.oracle.xxsh.ReturnMtxIndex</servlet-class>
<init-param>
<param-name>PAGE_JSP_THIS</param-name>
<param-value>/return_mtx_index.jsp</param-value>
</init-param>
</servlet>
<servlet>
<servlet-name>ReturnMtxIndexPocket</servlet-name>
<servlet-class>de.xxx.oracle.xxsh.ReturnMtxIndex</servlet-class>
<init-param>
<param-name>PAGE_JSP_THIS</param-name>
<param-value>/return_mtx_index_pocket.jsp</param-value>
</init-param>
</servlet>
<servlet>
<servlet-name>RcvMtxIndex</servlet-name>
<servlet-class>de.xxx.oracle.xxsh.RcvMtxIndex</servlet-class>
</servlet>
<servlet>
<servlet-name>RcvMtxOverview</servlet-name>
<servlet-class>de.xxx.oracle.xxsh.RcvMtxOverview</servlet-class>
</servlet>
<servlet>
<servlet-name>RcvMtxItem</servlet-name>
<servlet-class>de.xxx.oracle.xxsh.RcvMtxItem</servlet-class>
</servlet>
<servlet>
<servlet-name>RcvMtxBook</servlet-name>
<servlet-class>de.xxx.oracle.xxsh.RcvMtxBook</servlet-class>
</servlet>
<servlet>
<servlet-name>RcvMtxClose</servlet-name>
<servlet-class>de.xxx.oracle.xxsh.RcvMtxClose</servlet-class>
</servlet>
<servlet>
<servlet-name>RcvJitIndex</servlet-name>
<servlet-class>de.xxx.oracle.xxsh.RcvJitIndex</servlet-class>
</servlet>
<servlet>
<servlet-name>PalletPick</servlet-name>
<servlet-class>de.xxx.oracle.xxsh.PalletPick</servlet-class>
</servlet>
<servlet>
<servlet-name>RcvBBIndex</servlet-name>
<servlet-class>de.xxx.oracle.xxsh.RcvBBIndex</servlet-class>
</servlet>
<servlet>
<servlet-name>ZPixCall</servlet-name>
<servlet-class>de.xxx.oracle.xxsh.util.ZPixCall</servlet-class>
</servlet>
<servlet>
<servlet-name>XtimeLogin</servlet-name>
<servlet-class>de.xxx.oracle.xxsh.XtimeLogin</servlet-class>
<init-param>
<param-name>PAGE_JSP_THIS</param-name>
<param-value>/xtime_login.jsp</param-value>
</init-param>
</servlet>
<servlet>
<description>
</description>
<display-name>DoSo</display-name>
<servlet-name>DoSo</servlet-name>
<servlet-class>de.xxx.oracle.xxsh.DoSo</servlet-class>
</servlet>
<servlet>
<description>
</description>
<display-name>DlcClearingPoint</display-name>
<servlet-name>DlcClearingPoint</servlet-name>
<servlet-class>de.xxx.oracle.xxsh.DlcClearingPoint</servlet-class>
</servlet>
<servlet>
<description>
</description>
<display-name>DlcKidToLoc</display-name>
<servlet-name>DlcKidToLoc</servlet-name>
<servlet-class>de.xxx.oracle.xxsh.DlcKidToLoc</servlet-class>
</servlet>
<servlet>
<description>
</description>
<display-name>DlcKeToKid</display-name>
<servlet-name>DlcKeToKid</servlet-name>
<servlet-class>de.xxx.oracle.xxsh.DlcKeToKid</servlet-class>
</servlet>
<servlet>
<description>
</description>
<display-name>DlcInvoice</display-name>
<servlet-name>DlcInvoice</servlet-name>
<servlet-class>de.xxx.oracle.xxsh.DlcInvoice</servlet-class>
</servlet>
<servlet>
<description>
</description>
<display-name>DlcStatusMonitor</display-name>
<servlet-name>DlcStatusMonitor</servlet-name>
<servlet-class>de.xxx.oracle.xxsh.DlcStatusMonitor</servlet-class>
</servlet>
<servlet>
<description>
</description>
<display-name>BagScanStartLocation</display-name>
<servlet-name>BagScanStartLocation</servlet-name>
<servlet-class>de.xxx.oracle.xxsh.BagScanStartLocation</servlet-class>
</servlet>
<servlet>
<description>
</description>
<display-name>ScanBag</display-name>
<servlet-name>ScanBag</servlet-name>
<servlet-class>de.xxx.oracle.xxsh.ScanBag</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Root</servlet-name>
<url-pattern>/index</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Info</servlet-name>
<url-pattern>/info</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>testreplier</servlet-name>
<url-pattern>/testreplier</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>CycleCountIndex</servlet-name>
<url-pattern>/cycle_count_index</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>CycleCountLocator</servlet-name>
<url-pattern>/cycle_count_locator</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>CycleCountAddItem</servlet-name>
<url-pattern>/cycle_count_add_item</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>SimpleCycleCountIndex</servlet-name>
<url-pattern>/simple_cycle_count_index</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>SimpleCycleCountLocator</servlet-name>
<url-pattern>/simple_cycle_count_locator</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>SimpleCycleCountAddItem</servlet-name>
<url-pattern>/simple_cycle_count_add_item</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ReturnMtxIndex</servlet-name>
<url-pattern>/return_mtx_index</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ReturnMtxIndexPocket</servlet-name>
<url-pattern>/return_mtx_index_pocket</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>RcvMtxIndex</servlet-name>
<url-pattern>/rcv_mtx_index</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>RcvMtxOverview</servlet-name>
<url-pattern>/rcv_mtx_overview</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>RcvMtxItem</servlet-name>
<url-pattern>/rcv_mtx_item</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>RcvMtxBook</servlet-name>
<url-pattern>/rcv_mtx_book</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>RcvMtxClose</servlet-name>
<url-pattern>/rcv_mtx_close</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>RcvJitIndex</servlet-name>
<url-pattern>/rcv_jit_index</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>PalletPick</servlet-name>
<url-pattern>/pallet_pick</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>XtimeLogin</servlet-name>
<url-pattern>/xtime_login</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>RcvBBIndex</servlet-name>
<url-pattern>/rcv_bb_index</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ZPixCall</servlet-name>
<url-pattern>/zpixcall</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>DoSo</servlet-name>
<url-pattern>/DoSo</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>DlcClearingPoint</servlet-name>
<url-pattern>/dlc_clearing_point</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>DlcKidToLoc</servlet-name>
<url-pattern>/dlc_kid_to_loc</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>DlcKeToKid</servlet-name>
<url-pattern>/dlc_ke_to_kid</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>DlcInvoice</servlet-name>
<url-pattern>/dlc_invoice</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>DlcStatusMonitor</servlet-name>
<url-pattern>/dlc_statusmonitor</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>BagScanStartLocation</servlet-name>
<url-pattern>/bag_scan_start_location</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ScanBag</servlet-name>
<url-pattern>/scan_bag</url-pattern>
</servlet-mapping>
<resource-ref>
<description>description</description>
<res-ref-name>jdbc/ORACLE_APPS</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
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> My first JSP </title>
</head>
<body>
<form action="BagScanStartLocation" method="get">
Please enter a color <br>
<input type="text" name="color"size="20px">
<input type="submit" value="submit">
</form>
</body>
</html>
答案 0 :(得分:0)
这是因为在你的doGet()中你调用了doPost()方法;
答案 1 :(得分:0)
似乎web.xml中的配置有误。我从头开始构建项目并且它可以工作。
非常感谢。