Showing posts with label Java-EE. Show all posts
Showing posts with label Java-EE. Show all posts

superclass "javax.servlet.http.HttpServlet" not found on Java Build Path - solution

You might (normally) get the error following error on a dynamic java web project created through maven on Eclipse IDE. The solution is simple :
Error :
The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path   
Error Location :
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
Solution 1)
Edit pom.xml to include servlet-api-x.x.jar in your dependencies:
<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>javax.servlet-api</artifactId>
  <version>3.1.1</version>
  <scope>provided</scope>
</dependency>   
Solution 2)
Go to Project->Properties->Target Runtimes . And add your server container eg. : Apache Tomcat


The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path - Solution

Solution to the Java Web Project error : " The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path ".

Add the javax.servlet-api library (servlet.jar) to class path. If you're using maven add the following dependency ( scope = provided, runtime dependency will be provided by the servlet container i.e your web server eg : tomcat, jboss etc)



        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>

java spring - read properties file variable from xml -PropertyPlaceholderConfigurer

Java springframework xml configuration file - how to read properties file variables from spring xml :
We have to use PropertyPlaceholderConfigurer bean for this.

1).properties file location -

  • src/main/resource @ maven managed project
  • OR at classpath

2)The xml code to initialize/read properties file, 

hibernate annotation inheritance mappedsuperclass - common columns in super class

When you are using annotations for hibernate object relational mapping, there might be the case that we need to abstract out some common columns that goes into all table definitions. These columns might be ID, DFlag, LastModifiedDate etc..
In such case we can take advantage of @MappedSuperclass annotation to achieve inheritance in hibernate annotation.

Example :

Super class BaseTable that contains common column definitions:

@MappedSuperclass
public abstract class BaseTable {
    @Id
    @GeneratedValue
    @Column(name = "id")
    private int id;

    @Column(name = "dflag")
    private int dFlag;

    @Column(name = "lastmodifieddate")
    private Date lastModifiedDate;
    
    //other required columns
....
}

Extending it to use in other tables :


@Entity
@Table(name = "LoginUser")
public class LoginUser extends BaseTable  implements Serializable{

    private static final long serialVersionUID = -1920053571118011085L;

    @Column(name = "username")
    private String username;

    @Column(name = "password")
    private String password;

    @Column(name = "invalidCount")
    private int invalidCount;
    
    //other required tables
   ...
}


It works !

mysql hibernate unicode support - character set, collate

I just did following configurations to achieve Unicode support in my Java+Hibernate+MySQL project.

Configuring MySQL to support Unicode - set Character Set and Collate as :
CREATE TABLE YOUR_DB_NAME
 CHARACTER SET "UTF8"
 COLLATE "utf8_general_ci";

NOTE : You Need to do "ALTER TABLE" instead of "CREATE TABLE", 
      if you are going to modify existing DB.

Hibernate JDBC connection string :
jdbc.url=jdbc:mysql://localhost:3306/YOUR_DB_NAME?useUnicode=true&characterEncoding=UTF-8

Hibernate Configuration:
<hibernate-configuration>
<session-factory>
    ...
    <property name="hibernate.connection.charSet">UTF-8</property>
    <property name="hibernate.connection.characterEncoding">UTF-8</property>
    <property name="hibernate.connection.useUnicode">true</property>
    ...
</session-factory>
</hibernate-configuration>

Java code to find public IP address (servlet and client side code)


Java code to find public IP address :

URL url= new URL("http://gt-tests.appspot.com/ip");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String ip = in.readLine();
System.out.println("IP : "+ip);

I created a simple servlet app on google app engine  and posted at http://gt-tests.appspot.com/ip .

The servlet code returns the public address of client, it looks like :

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
  PrintWriter out = resp.getWriter();
  // Get client's IP address
  String addr = req.getRemoteAddr();
  out.println(addr);
  ...

hibernate show sql & parameter to console


You need to configure it 2 places :
1) Configuration in log4j logger : add following lines in - log4j.properties file  :
log4j.logger.org.hibernate.SQL=DEBUG
log4j.logger.org.hibernate.type=TRACE
The first is equivalent to hibernate.show_sql=true, the second prints the bound parameters among other things.
2)Configuration in hibernate.cfg.xml :

<property name="show_sql">true</property>
TO Show Formatted SQL :<property name="format_sql">true</property>

java escape html string - code

1) StringEscapeUtils from Apache Commons Lang:

import static org.apache.commons.lang.StringEscapeUtils.escapeHtml;
// ...
String source = "The less than sign (<) and ampersand (&) must be escaped before using them in HTML";
String escaped = escapeHtml(source);


OR 

2) Use Spring's HtmlUtils.htmlEscape(String input) method.