在jsp上创建/显示servlet响应

时间:2018-09-05 11:42:32

标签: java jsp

我不得不问你,因为我被卡住了,不能继续下去。我正在尝试使响应路径如下:

DAO-> Servlet-> JSP

过程是:用户在jsp上填写表单,单击“提交”,并在同一JSP上获得响应(用户已添加或未添加)

向DB添加数据工作正常,但是WebBrowser出现了错误:

类型异常报告

消息无法解析参数编号:pageContext.request.contextPath

说明服务器遇到意外状况,阻止其满足请求。

这是我的jsp代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix ="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix ="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix ="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<%@ page isELIgnored="false"%>
<script type="text/javascript" src="${pageContext.request.contextPath}/jQuery.js"/></script>

<!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>dodaj pracownika</title>
</head>
<body>
<f:view>

<section id = registration" class = "section">
<div class "container tagline">
<em>{0}</em>


</div>
<br>

<AdContentProvider
Name="MIME-type"
Provider="addUser.class"
Properties="optional-properties-for-your-class"
>
</AdContentProvider>

<form action="${pageContext.request.contextPath}/newuser" method = "post">

<form>
<label> Imie</label> <input type="text" name= "imie" id="imie"> <br/>
<label> Nazwisko</label> <input type="text" name= "nazwisko" id="nazwisko"> <br/>
<label> Stanowisko</label> <input type="text" name= "stanowisko" id="stanowisko"> <br/>
<label> Stawka</label> <input type="text" name= "stawka" id="stawka"> <br/>

<input type ="submit" value="Dodaj" id = "dodaj">

</form>







</f:view>
</body>
</html>

和servlet代码:

package tk.jewsbar.servlets;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.text.MessageFormat;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.Transactional;

import fasolki.Pracownik;
import tk.jewsbar.dao.Dao;



@WebServlet("/newuser")
public class addUser extends HttpServlet 
{

@Override
@Transactional
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{


    String imie = req.getParameter("imie");
    String nazwisko = req.getParameter("nazwisko");
    String stanowisko = req.getParameter("stanowisko");
    double stawka = Double.valueOf(req.getParameter("stawka"));




    Pracownik pracownik = new Pracownik (imie,  nazwisko,  stanowisko, stawka);
    Dao dao = new Dao();


    int rows = dao.dodajUzytkownika(pracownik);







    String err = "null";


if (rows ==0)
{
    err = "Niestety nie udalo sie dodac uzytkownika";
}

else {

    err = "Dodano uzytkownika" + rows;
}

String pejcz = getHTMLString(req.getServletContext().getRealPath("html/newuser.jsp"), err);
resp.getWriter().write(pejcz);


}

public String getHTMLString(String sciezka, String message) throws IOException
{

    BufferedReader czytaj = new BufferedReader (new FileReader(sciezka));

    String linia = "";

    StringBuffer bufor = new StringBuffer();

    while((linia=czytaj.readLine())!= null)

            {
        bufor.append(linia);

            }

    czytaj.close();

        String pejcz = bufor.toString();

        pejcz = MessageFormat.format(pejcz,  message);


        return pejcz;

            }




    @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

            String pejcz = getHTMLString(req.getServletContext().getRealPath("html/newuser.jsp"),"");
            resp.getWriter().write(pejcz);
        }

}

2 个答案:

答案 0 :(得分:0)

尝试这样

<script type="text/javascript"  src="<c:out value="${contextPath}"/>/jQuery.js"></script>

<script type="text/javascript"  src="<c:out value="${pageContext.request.contextPath}"/>/jQuery.js"></script>

代替

<script type="text/javascript" src="${contextPath}/jQuery.js"/></script>

您可以阅读此链接上的建议, expression-language-in-jsp-not-working

答案 1 :(得分:0)

我已经更改了它,现在出现了这样的错误:

  

类型异常报告

     

消息无法解析参数编号:contextPath

     

说明服务器遇到意外状况,阻止其满足请求。