我刚开始学习servlets yersterday所以我是新手。我阅读了一个教程,并制作了以下程序来跟踪链接的使用:
package red;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/Redirection")
public class Redirection extends HttpServlet {
private static final long serialVersionUID = 1L;
private String referrer;
private String target;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
getURLs(request);
}
catch(Exception e)
{
response.sendError(500, "Target parameter not specified");
return;
}
response.sendRedirect(target);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
public void getURLs(HttpServletRequest request)
{
referrer = request.getParameter("referrer");
if(referrer == null || 0 == referrer.length())
{
referrer = new String("");
}
target = request.getParameter("target");
if(target == null || target.equals(""))
{
throw new IllegalArgumentException();
}
}
}
但是当我测试它(Eclipse与Tomcat)时,我得到了这个:
HTTP Status 500 - Target parameter not specified
如何在eclipse中指定目标参数,以便运行此程序?
对不起初学者的问题。
答案 0 :(得分:1)
嗯,你真的不知道这里发生了什么。也许你得到了一个不同的例外 - 无论出现什么问题,你都会给出错误信息。你应该准确记录被抛出的内容。您通常也不应该自己捕获Exception
- 捕获更具体的例外。
无论如何,通常要包含那种参数,你只需将它放在URL中:
/Redirect?target=x&referrer=y