2017-02-17 37 views
1

需要构建应用程序而不使用在Java3中使用的注释并且在开始时遇到一些问题。看起来像web.xml/view/controller中的设置是合乎逻辑的并应该起作用。给出一个404错误,如果我使用注释它不。我想这就是为什么赋值状态不使用注释!任何关于S.O的旧定时器的建议?Java Web应用程序不使用注释

P.S任何建议将真正帮助我开始这个应用程序,我在网上阅读,我已阅读的解决方案似乎并没有工作。而最终建议使用注释...我不能使用!

的welcome.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" 
    pageEncoding="UTF-8"%> 
<!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=UTF-8"> 
<title>Welcome Page</title> 
</head> 
<body> 

<h1>Welcome Page</h1> 

<br/> 

<form action="/OptionsForYou" name="options"> 

<select> 
    <option>---Select---</option> 
</select> 

<button type="submit" value="submit">Submit</button> 

</form> 

</body> 
</html> 

OptionsWork.java

package com.server; 

import java.io.IOException; 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

/** 
* Servlet implementation class OptionsWork 
*/ 

public class OptionsWork extends HttpServlet { 
    private static final long serialVersionUID = 1L; 

    /** 
    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 
    */ 
    protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { 
     String options = req.getParameter("options"); 
     System.out.println(options); 
    } 

} 

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> 
    <display-name>Options</display-name> 
    <welcome-file-list> 
    <welcome-file>index.html</welcome-file> 
    <welcome-file>index.htm</welcome-file> 
    <welcome-file>index.jsp</welcome-file> 
    <welcome-file>default.html</welcome-file> 
    <welcome-file>default.htm</welcome-file> 
    <welcome-file>default.jsp</welcome-file> 
    </welcome-file-list> 
    <servlet> 
     <servlet-name>OptionsWork</servlet-name> 
     <servlet-class>com.server.OptionsWork</servlet-class> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>OptionsWork</servlet-name> 
     <url-pattern>/OptionsForYou</url-pattern> 
    </servlet-mapping> 
</web-app> 
+0

交叉检查您的依赖关系。 – Akshay

+0

尝试在实现中指定load-on-startup和Sysout以确保servlet正在加载或未加载。也只是一个想法,您应该在

中指定请求方法,以便在servlet上执行正确的方法。 –

回答

0

的事情是,为u如果您的webapp部署在ROOT上下文中,则必须在任何链接中指定上下文路径,包括表单的action属性。

比方说,您的应用程序(在$TOMCAT_ROOT/webapps/myApp文件夹即在Tomcat)的部署myApp,那么你的welcome.jsp具有URL http://localhost:8080/myApp/welcome.jspOptionsWork servlet的“坐”在http://localhost:8080/myApp/OptionsForYou URL。但是,如果您只在<form action="/OptionsForYou" name="options">中指定/OptionsForYou,那么您可以拨打http://localhost:8080/OptionsForYou,这确实不存在,并且您会收到404错误。

所以,的/OptionsForYou前添加<%= request.getContextPath() %>包括在操作上下文路径,即你的<form>标记看起来应该

<form action="<%= request.getContextPath() %>/OptionsForYou" name="options"> 

,它应该工作。

相关问题