Cannot resolve method getParameter() in JSP - java

I'm building a small project in JSP. I want to take data from a HTML sign up form and save them to a database. But my IDE (intellij) won't allow me to do so because of the error in the title. Does anyone know a fix to this? Internet research didn't really helped me.
Thanks in advance!
EDIT
<%
String name = request.getParameter("realName");
%>
Error: Cannot resolve method 'getParameter(java.lang.String)'.

I'm assuming that your JSP file looks like this:
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<%
String name = request.getParameter("realName");
%>
Here's the param "realName": <%=name%>
</body>
</html>
And that it looks like this, in your IntelliJ:
If that's the case, I'm almost sure you're missing the servlet-api.jar file in your classpath.
Here's one of the ways to add it on IntelliJ:
Right-click on your project and select Open Module Settings:
Make sure that you're on the Modules section, Dependencies tab, click on the "+" button at the bottom, and select 1 JARs or directories...:
Select the file servlet-api.jar from the folder lib at (THIS IS IMPORTANT:) the container where you're deploying your application (in my case, apache-tomcat-8.5.31):
Then click on the "Ok" button. Your program now should look like this:
You're good to go!
I hope it helps.
Note: I know that sometimes you cannot avoid to use scriptlets, especially when you're working on legacy codes, as I did for a while. Even though, please also pay attention to the other answers here about using scriptlets. There are several other options available.

As a complement to the answer, try this on your intellij ide :
add support framework
check maven
go to pom.xml
add servlet-api dependency :
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>

Related

JSTL tags are not working in eclipse

I am new to JSTL, I downloaded jstl-1.2.jar from here to my web-content/web-inf/lib folder.
In my JSP file, I have mentioned:
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
but everytime when I tries to use any JSTL tag, I got the error:
HTTP Status 500 - java.lang.NoClassDefFoundError:
javax/servlet/jsp/tagext/TagLibraryValidator
I am using eclipse photon, tomcat server-8.0.52.
What should I do? I have searched a lot on the internet about this, but I got the same errors. Please help me.
It seems that you are getting error during runtime!
If you are using maven you must add JSTL dependency
or if you are not using maven or gradle you must add the jar to the class path.
Oh sorry! TagLibraryValidator its your problem!
add jsp-api-2.2.jar to your lib folder.
http://www.java2s.com/Code/Jar/j/Downloadjspapi22jar.htm
At last, I got the solution. I just reinstall my java, eclipse, server and reconfigure jstl-1.2.jar. It start working well. I don't know how but this is only solution for me.
ADD FOLLOWING DEPENDENCIES TO A WEB APPLICATION
To use this distribution with your own web applications, add the following JAR
files to the '/WEB-INF/lib' directory of your application:
taglibs-standard-spec-1.2.5.jar
taglibs-standard-impl-1.2.5.jar
taglibs-standard-jstlel-1.2.5.jar
xalan-2.7.1.jar
serializer-2.7.1.jar
If you do not use JSTL 1.0 tags then the "taglibs-standard-jstlel" JAR may be
omitted. If you do not use the XML library, then the Apache Xalan dependencies
may also be omitted.
Sometimes only taglibs-standard-impl-1.2.5.jar library is enough. But if you want to use like forEach loop you should add two libraries to WEB-INF/lib of your project: Impl: taglibs-standard-impl-1.2.5.jar Spec: taglibs-standard-spec-1.2.5.jar
https://downloads.apache.org/tomcat/taglibs/taglibs-standard-1.2.5/README_bin.txt

Spring resources folder

I have a Maven project which use Spring . In this project have a folder called res(in src/main/webapp), which contain resources(css, images, js, etc.). This resources are called in JSP files.
My problem is this: I try to put an image logo.png on index.jsp:
<img src="/res/images/logo.png"/>
In page appear this: Page Result
Here is how my project look: Tree project
How can I access logo.png?
Unless you deploy your webapp as the root webapp in your server, the path of the image is wrong. It should start with the context path of your application:
<img src="${pageContext.request.contextPath}/res/images/logo.png"/>
If you mapped the spring servlet to /, you also need to make these resources available, either by enabling the default servlet, or by mapping the resources as explained in the documentation
Try this:
<mvc:resources mapping="/resources/**" location="/resources/folder_name/"/>
and also i recommend use jstl taglib
For example add dependency :
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
and in your jsp you can use this approach instead you default:
<c:url value="src="images/logo.png"/>
good luck

My IntelliJ do not hint the out's method in JSP

I written a hello.jsp:
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<%# page import="java.util.*, java.text.* , java.lang.String" %>
<html>
<head>
<title>My JSP `hello.jsp` starting page</title>
</head>
<body>
<%
// 写java代码
SimpleDateFormat sdf = new SimpleDateFormat();
String curDate = sdf.format(new Date());
//print the information
out.print("hello"+curDate); // Here, my IntelliJ did not hint the print method, and also show Cannot resolve method 'print'
%>
</body>
</html>
The error show:
Cannot resolve method 'print(java.lang.String)'
The snapshot, you can see the print method is red color:
But I run this jsp in my browser, it prints the information in my browser.
I was having same issue on IntelliJ Ultimate 2020.3.2. Then I put the dependency mentioned by leela Daka on my pom.xml, reloaded the pom and It worked! The only change I've made was on version to 2.2 to match with Tomcat 7 that uses JSP 2.2.
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope></dependency>
<dependency>
You should include 'lib' directory of Tomcat into your project.
If using IntelliJ IDE: file->project structure->libraries-> + ->[lib folder of your tomcat]
Add this dependency in the pom.xml file
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope></dependency>
And run clean, install cmds from mvn life cycle.
An option will appear in intellij ide on top right asking to re load the project click on that and it removes this error in Intellij ide.

JSP cannot find default bundle for i18n

After browsing through the whole internet I ended up asking this question, although I find it a bit difficult to describe the situation.
I have a little application here which runs on embedded Tomcat server (v7), and uses servlets and JSPs; I try to internationalize them with JSTL tags. The final project is deployed as JAR, and when I run it from the console with java -jar, the embedded server starts nicely, everything works just fine.
The problem is when I try to run it in the IDE (I use IntelliJ Idea v13.1.2): again, it starts, but instead of the values from the bundle, the pages show values such as ???default.username???.
Here is how my JSPs mostly look like:
<!DOCTYPE html>
<%# taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%# page contentType="text/html;charset=UTF-8" %>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="language"
value="${not empty param.language ? param.language : not empty language ? language : pageContext.request.locale}"
scope="session"/>
<fmt:setLocale value="${language}"/>
<fmt:setBundle basename="messages" scope="session" var="bund"/>
<html>
<head>
<title><fmt:message bundle="${bund}" key="default.title" /></title>
<link rel="stylesheet" type="text/css" href="../css/tdb.css" media="all">
</head>
And so on. The <fmt:message bundle="${bund}" key="default.title" /> and similar parts work perfectly fine when I use the JAR, and result in ???default.title??? when from IDE. In one case I use the bundle file from the servlet, and when ran from JAR, it works fine, and when from IDE, it causes java.util.MissingResourceException.
What have I tried so far? I added my messages.properties and messages_en_US.properties files in various locations (in resources folder, on the same level with the java and webapp folders; in separate package in the com.my.example package; as simple properties files in the com.my.example package), tried to refer to it only with the basename (resourceBundle = ResourceBundle.getBundle("messages", locale);), or with the fully qualified path; also, I set the fallbackLocale and localizationContext parameters in the web.xml file.
What am I missing?
Code looks well.
I was checking in some of my projects and I have this attributes:
<fmt:setBundle basename="org.juanitodread.msg.label" var="label"/>
<fmt:message key="common.title" bundle="${label}" />
I have my projects in Eclipse, but you can try without "scope" attribute. My label.properties file is in "org.juanitodread.msg" package.
I also use the Intellij Idea and had the similar problems, here are my conclusions. You should put your 'messages file' to
yourproject/src/main/java/resources
folder (as a rule, Idea will highlight the resources folder icon with the specified sign). I have no fallback locale configuration and localization context parameters in web.xml. My bundle file is named messages_en.properties and I use it the way
<fmt:setBundle basename="messages" var="labels" />
and not add "resources.messages" to basename attribute.
I'm running IntellijIdea version 2016.2, and my application works fine on:
embedded Tomcat v 7.0.73 , using bmuschko gradle-tomcat plugin https://github.com/bmuschko/gradle-tomcat-plugin
remote Tomcat v 7.0.73 deployment with Tomcat Server Idea configuration
To check your resources are really loading (if your properties are not loaded to the page that means you're obviously missing the file), use the following code, as mentioned in https://stackoverflow.com/a/4137991/2759640
ClassLoader ctxLoader = Thread.currentThread().getContextClassLoader();
URL propsURL = ctxLoader.getResource("opto-mapping.properties");
URLConnection propsConn = propsURL.openConnection();
In case you have your resources loaded, openConnecton will throw an exception, that connection to the messages.properties has been already opened (at least I've done that way and tried to make my bundle work also for a long time).

pre-compile jsp files happend exceptions

I pre-compile some jsp files through ant task of jspc,but it built failed.
errers:
info.jsp(35,2) The attribute prefix fn does not correspond to any imported tag library
info.jsp line 35 :
<c:if test="${fn:length(requestScope.checkDetailInfoList) gt 1}">
ant task xml:
<jasper validateXml="false" uriroot="${basedir}/WebRoot"
webXmlFragment="${dir.WEB-INF}/generated_web.xml"
outputDir="${dir.WEB-INF}/src" />
How should I correct?
You need to make sure the jsp file imports the fn namespace of the JSTL. You'll need a line that looks something like this in your jsp file:
<%# taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
Also, you'll need to make sure the JSTL jars (jstl.jar and standard.jar) are in your classpath when jasper tries to compile.
Asaph's comments are spot on.
There's one other bit to check: The <fn> tag set was a later addition to the JSTL libraries. Maybe you have an older version of jstl.jar and standard.jar that needs to be updated.

Categories