2

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)'.

Marios Koni
  • 143
  • 1
  • 13

2 Answers2

13

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:

enter image description here

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:

  1. Right-click on your project and select Open Module Settings:

enter image description here

  1. Make sure that you're on the Modules section, Dependencies tab, click on the "+" button at the bottom, and select 1 JARs or directories...:

enter image description here

  1. 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):

enter image description here

  1. Then click on the "Ok" button. Your program now should look like this:

enter image description here

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.

Almir Campos
  • 2,833
  • 1
  • 30
  • 26
2

As a complement to the answer, try this on your intellij ide :

  1. add support framework
  2. check maven
  3. go to pom.xml
  4. 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> 
    
ans1genie
  • 429
  • 1
  • 6
  • 16
  • 1
    +1 for the maven dependency. The key point is the tag "". You can find more details at https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Scope Please, pay attention to this one: "This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime" – Almir Campos Oct 05 '18 at 21:40