我正在使用Spring Tool Suite,我希望从sevelet重定向到jsp(rapports.jsp)。但我不能这样做。我收到此错误:
HTTP状态404 - /rapports.jsp 类型状态报告
消息:/rapports.jsp
description:请求的资源不可用。
这是我的控制器:
package com.mycompany.myapp;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.http.HttpStatus;
import javax.servlet.ServletConfig;
import java.io.*;
import java.util.*;
import javax.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.tools.ant.util.Base64Converter;
import javax.servlet.ServletConfig;
@Controller
public class HomeController extends HttpServlet {
private static final long serialVersionUID = 1L;
@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView adminPage() {
ModelAndView model = new ModelAndView();
model.setViewName("authentification");
return model;}
@RequestMapping(method=RequestMethod.POST)
public void service(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException{
String Str1 = request.getParameter("smya");
String Str2 = request.getParameter("mdps");
System.out.println(Str1);
System.out.println(Str2);
try {
URL url = new URL("http://pc-demo-bi:8090/jasperserver-pro/rest/login?j_username="+Str1+"&j_password="+Str2);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
System.out.println("connected");
response.sendRedirect("/rapports.jsp");
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
as you see I have used: response.sendRedirect("/rapports.jsp") to do so.
,这是我的Web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
这是我的rapport.jsp
<title>hahahah</title>
</head>
<body>
Welcome!!
</body>
</html>
如果您有任何建议,请提前感谢。
答案 0 :(得分:0)
就个人而言,我认为你不需要扩展HttpServlet。尝试类似我下面的内容。假设您有一个有效的视图解析器(Spring或其他,我更喜欢Apache Tiles),只返回JSP页面的名称“rapports”将重定向用户。您还可以使用注释@RequestAttribute从HttpServletRequest获取Str1和Str2,而不是将其注入到方法中。
@Controller
public class HomeController {
private static final long serialVersionUID = 1L;
@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView adminPage() {
ModelAndView model = new ModelAndView();
model.setViewName("authentification");
return model;
}
@RequestMapping(method=RequestMethod.POST)
public void service(@RequestAttribute("smya") Str1, @RequestAttribute("mdps") Str2){
System.out.println(Str1);
System.out.println(Str2);
try {
URL url = new URL("<code>http://pc-demo-bi:8090/jasperserver-pro/rest/login?j_username="+Str1+"&j_password="+Str2</code>);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
System.out.println("connected");
return("rapports");
conn.disconnect();
}
catch (MalformedURLException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
希望这是有道理的!