不能在java servlet

时间:2017-05-30 05:00:49

标签: java jsp servlets

我刚开始学习servlet并遇到了以下问题。 我需要在我的servlet中使用 forward 方法,并在其他框架中显示结果。 但没有显示任何内容。当我使用 include 方法时,它会向我显示结果。 如何使用前进方法显示结果?

frame.jsp

<%@ page import="StudyC.C18_HelloWeb"%>
<%@ page language="java" contentType="text/html;  charset=Windows-31J"
pageEncoding="Windows-31J"%>
<html>
<head>
<title>検索画面</title>
</head>

<FRAMESET ROWS ="20%,80%" >
    <FRAME SRC ="C18_HelloINPUT.jsp" NORESIZE scrolling = yes>
    <FRAME SRC ="C18_HelloOUTPUT.jsp" NORESIZE scrolling = no name = frameOutput>
</FRAMESET>

C18_HelloINPUT.jsp

<%@ page language="java" contentType="text/html;  charset=Windows-31J"
pageEncoding="Windows-31J"%>
<%@ page import = "javax.servlet.RequestDispatcher" %>
<html>
<script>
    function validateForm() {
    var x = document.forms['myForm']['dataName'].value;
    if (x == null || x.match(/^\s*$/)) {
            alert('空です');
            return false;
        }
    }
</script>
<style>
h1 {text-align:center;}
</style>

    <h1>入力画面</h1><br>
        <form name='myForm' action="C18_HelloWeb" method="get"
            onsubmit='return validateForm()'target='frameOutput' >
            <input type='text' name='dataName'>
            <input type='submit' value='クエリ送信'>
        </form>

C18_HelloOUTPUT.jsp

<%@ page language="java" contentType="text/html;  charset=Windows-31J"
pageEncoding="Windows-31J"%>

<html>
<style>
    h1 {text-align:center;}
</style>

<h1>出力画面</h1>
</html>

C18_HelloWeb.java

package StudyC;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class C18_HelloWeb extends HttpServlet {

/**
 *
 */
private static final long serialVersionUID = 1L;
public void init(ServletConfig config)
throws ServletException{
}

protected void doGet (HttpServletRequest request, HttpServletResponse respone)
throws ServletException, IOException {

    respone.setContentType("text/html; charset=Windows-31J");
    request.setCharacterEncoding("Windows-31J");

    HttpSession session = request.getSession(true);
    PrintWriter out = respone.getWriter();
    RequestDispatcher rd = request.getRequestDispatcher("C18_HelloOUTPUT.jsp");
    rd.forward(request,respone);

    String firstName = request.getParameter("dataName");

    @SuppressWarnings("unchecked")
    ArrayList<String> dataList = (ArrayList<String>)session.getAttribute("PreviousItems");
    if( dataList == null ) {
        dataList = new ArrayList<String>();
        session.setAttribute("PreviousItems", dataList);
    }
    dataList.add(firstName);

    int i = 0;
    do{
        out.print(HTMLFilter.filter(dataList.get(i)) + "<br>");
        i++;
    } while(i<dataList.size());
}

protected void doPost(HttpServletRequest request, HttpServletResponse respone)
throws IOException, ServletException
{
    doGet(request, respone);

}
}

我认为 C18_HelloINPUT.jsp C18_HelloWeb.java 存在问题。谢谢!

0 个答案:

没有答案