Friday 4 October 2013

Fatal error: Call to undefined function http_redirect() : Fixed

Issue: When I was trying to call the PHP http_redirect() function, below error came up:

Fatal error: Call to undefined function http_redirect() in location.


Add the below function in your php page

/**
 * redirect to a specific URL
 * @param $url
 */
     
function redirect($url)
{
     if (!
headers_sent())
     {  
          
//If headers not sent yet... then do php redirect
          
header('Location: '.$url); exit;
     }
     else
     {                  
          
//If headers are sent... do javascript redirect...
          //if javascript disabled, do html redirect.
          
echo '<script type="text/javascript">';
          echo 
'window.location.href="'.$url.'";';
          echo 
'</script>';
          echo 
'<noscript>';
          echo 
'<meta http-equiv="refresh" content="0;url='.$url.'" />';
          echo 
'</noscript>'; exit;
     }
}  


then call the above function  to the page you wish the redirect to happen on

eg: redirect("redirect.php");




Friday 12 July 2013

Cannot access xampp from internet in Cent OS

Issue

I have xampp installed on Cent OS. I started the xampp server and tried to access it with localhost, it is working. But i cant access it using the system IP. I can ping the server.But I can't  access the server from any other computer on the network.

Solution

The default firewall of CentOS permits ssh input(tcp 22) and icmp(ping). Open a terminal and type


[root@ ~]# cat /etc/sysconfig/iptables
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
Try at first stopping your firewall issuing the following command:

[root@ ~]# /etc/init.d/iptables stop
Now, test if you can access your XAMPP server.
Go to File System -> etc - >sysconfig
Open iptables. Remove the below line


-A INPUT -j REJECT --reject-with icmp-host-prohibited
 Add the below new line

iptables -A INPUT -m state --state NEW -m tcp -p tcp -m multiport --dports 80,443 -j ACCEPT
It enables the ports you need to access(80 , 443).
Now your iptables looks like


# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp -m multiport --dports 80,443 -j ACCEPT
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
Save the file and close it.
Start your firewall issuing the following command:


[root@ ~]# /etc/init.d/iptables start
Now  you can access your XAMPP server using your macine ip address


Wednesday 26 June 2013

Eclipse donot generate a web.xml while creating dynamic web project

This is beacuse you have'nt check the checkbox for automatically generating the web.xml while creating dynamic web project

1. select File-->New-->Dynamic Web Project
2. After entering the name for your project, click next and next again to get to the "web module" window
3. Click the checkbox "generate web.xml deployment descriptor"

Creating servlet in eclipse with Tomcat

1. Create Dynamic Web Project
   Select from the menu File --> New --> Dynamic Web Project.
  

 2. Check 'Generate web.xml deployment descriptor' checkbox and click "Finish" button and Eclipse IDE will  generate the web project  automatically as shown below




3. Project "helloworld" is created.'


4. You need to add the Servlet API to your classpath.
    refer http://lekshmideepu.blogspot.in/2013/06/the-import-javaxservlet-cant-be-resolved.html
5. Create a Servlet Class
    Create a package under src in your project (say com.hello)
     Right click the package, select new-->servlet.(say HelloWorldServlet as class name).
     Click next and finish

6. Add the below code in 'doGet' method of HelloWorldServlet

          response.setContentType("text/html");
          PrintWriter printWriter  = response.getWriter();
          printWriter.println("<h1>Hello World!</h1>");

         So your HelloWorldServlet  looks like below

package com.hello;

import java.io.IOException;
import java.io.PrintWriter;

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

/**
 * Servlet implementation class HelloWorldServlet
 */
@WebServlet("/HelloWorldServlet")
public class HelloWorldServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * Default constructor.
     */
    public HelloWorldServlet() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
          response.setContentType("text/html");
          PrintWriter printWriter  = response.getWriter();
          printWriter.println("<h1>Hello World!</h1>");

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}



7. Create Servlet Mapping in Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
 
  <servlet>
        <display-name>helloworldservlet</display-name>
        <servlet-name>helloservlet</servlet-name>
        <servlet-class>com.hello.HelloWorldServlet</servlet-class>
  </servlet>
  <servlet-mapping>
     <servlet-name>helloservlet</servlet-name>
     <url-pattern>/hello</url-pattern>
  </servlet-mapping>

 
</web-app>

  
9. Add your project to "Tomcat" server and start the server

8. Open a browser and type url:
   http://localhost:8080/helloworld/hello




Friday 14 June 2013

HTTP Status 405 - HTTP method POST is not supported by this URL

Problem:

When i run my servlet by tomcat server, i got error message
HTTP Status 405 - HTTP method POST is not supported by this URL

Solution

The issue is beacause, you made a HTTP post request from jsp form, but you don't have a doPost() method in the servlet to handle it.
you need to add the below line your servlet class.

public void doPost(HttpServletRequest request, HttpServletResponse response)
 throws IOException{
  ....  
 }

Wednesday 12 June 2013

Several ports (8005, 8080, 8009) required by Tomcat Server at localhost are already in use

Issue

When you try to run a jsp program on Tomcat server in eclipse, you are getting the below error in the eclipse console
"Several ports (8005, 8080, 8009) required by Tomcat v6.0 Server at localhost are already in use."

Solution

The issue is because you've another instance of Tomcat already running.
To solve this 
1. Go to bin folder of Tomcat  (eg C:\apache-tomcat-7.0.23\bin )
2. Run startup.bat
3. Run shutdown.bat
4. Start the tomcat from eclipse
 

 

The import javax.servlet can't be resolved

Error : The import javax.servlet can't be resolved in eclipse

Soultion

You need to add the Servlet API to your classpath. In Tomcat 7.0, this is in a JAR called servlet-api.jar in Tomcat's lib folder.
steps to add the JAR into your project class path.

Right-click the project, click Properties.
Choose Java Build Path.
Click Add External JARs
Browse to find servlet-api.jar and select it.
Click OK to update the build path.
Clean and build the project