RequestDispatcher显示页面源代码Java Servlet

时间:2015-10-18 14:25:24

标签: java jsp servlets

一切正常,为什么RequestDispatcher显示页面的源代码?

public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException {
        String uName=req.getParameter("uEmail");
        String uPass=req.getParameter("uPass");
        try{
            DBConnection con=new DBConnection();
            if(con.login(uName, uPass)){
                HttpSession on = req.getSession();
                on.setAttribute("u_id", uName);
                res.sendRedirect("dashboard.jsp");
            }
            else{
                RequestDispatcher dis= getServletContext().getRequestDispatcher("/login.jsp");
                PrintWriter write = res.getWriter();
                write.println("Wrong Username or Passowrd");
                dis.include(req, res);
            }
        }catch(ClassNotFoundException | SQLException e){}

    }
}

页面重定向到指定网址/login.jsp并显示错误消息,但为什么作为源代码?

Wrong Username or Passowrd

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form action="LoginServlet" method="POST" />
            <input type="text" name="uEmail" />
            <br /><br />
            <input type="text" name="uPass" />
            <br /><br />
            <input type="Submit" name="Register" value="Register" />
            <br /><br />
        </form>
    </body>
</html>

指向login.jsp的直接链接正常。

3 个答案:

答案 0 :(得分:3)

来自http://docs.oracle.com/javaee/6/api/javax/servlet/RequestDispatcher.html#include(javax.servlet.ServletRequest,javax.servlet.ServletResponse)

Request Dispatcher的include方法获取您在浏览器中获取源代码的资源内容。

我认为您要做的是将请求转发给login.jsp,因此请使用请求调度程序的转发方法。

由于来自Servlet的消息,@ JBNizet在评论中提到,HTML将无效。

答案 1 :(得分:0)

您需要设置回复的内容类型。

$(selector).append(content)方法中添加以下行。

doPost

希望它适合你!!

答案 2 :(得分:0)

--- title: "test" output: flexdashboard::flex_dashboard: theme: bootstrap runtime: shiny --- ```{r setup, include=FALSE} library(flexdashboard) library(tidyverse) ``` ```{r global, include=FALSE} set.seed(1) dat <- data.frame(age = sample(15:99, 100, replace=TRUE), y = runif(100)) ``` Sidebar {.sidebar} ===================================== ```{r} # age sliderInput("agerange", label = "Age", min = 15, max = 99, value = c(15, 99), step=10) # age category selectInput("agecat", label = "Age Category", choices = list("All" = 1, "15-19" = 2, "20-24" = 3), selected = 1) observeEvent(input$agecat,{ range = c(15,99) if(input$agecat == 2) { range = c(15,19) } else if(input$agecat == 3) { range = c(20,24) } else { input$agecat } updateSliderInput(session, "agerange", value = range, min = min(range), max = max(range), step = 1) }) ``` Page 1 ===================================== Column ----------------------------------------------------------------------- ### Chart A ```{r} renderPlot({ dat %>% filter(age >= input$agerange[1] & age <= input$agerange[2]) %>% ggplot(., aes(y)) + geom_histogram() }) ``` 之前 将此行添加dis.include(req,res);