[英]Java Web App NOT using annotation
需要構建一個不使用> Java3中使用的注釋並且開始時有點麻煩的應用程序。 看起來像web.xml / view / controller中的設置是合乎邏輯的,應該可以工作。 給出404錯誤,如果我使用注釋則不會。 我想這就是為什么作業聲明不使用注釋! 來自舊計時器的任何建議嗎?
PS任何建議真的可以幫助我啟動這個應用程序,我正在網上閱讀,我讀過的解決方案似乎不起作用。 大多數人最終建議使用注釋......我無法使用!
的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>
問題是,除非您的Web應用程序部署在ROOT上下文中,否則您必須在任何鏈接中指定上下文路徑,包括表單的action
屬性。
假設您的應用程序部署為myApp
(即在$TOMCAT_ROOT/webapps/myApp
文件夾中的Tomcat中),然后您的welcome.jsp
具有URL http://localhost:8080/myApp/welcome.jsp
,並且OptionsWork
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">
它應該工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.