Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Getting Error in code pls help

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Getting Error in code pls help

    I am getting the inputs "distance" and "service " from a previous html page and passing the values here to calculate "estimateamt" and pass the calculated "estimateamt" to a jsp page "service2.jsp" using session attribute

    package packk;
    import java.io.*;
    import java.sql.*;
    import javax.servlet.*;
    import javax.servlet.http.*;



    public class query2 extends HttpServlet {


    protected void service(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();


    String s=request.getParameter("distance");
    String tr=request.getParameter("service");
    try{

    long d=Long.parseLong(s);

    HttpSession sess=request.getSession();

    sess.setAttribute("distance",s);
    sess.setAttribute("service",tr);




    String op1,op2,op3,op4,op5,op6;
    op1="Intra City Relocation";
    op3="Inter State Relocation";
    op5="Intra State Relocation";
    op2="Intra City Moving";
    op4="Inter State Moving";
    op6="Intra State Moving";

    long ea=0;

    sess.setAttribute("estimateamt",ea);


    if(d<=250)
    {
    if(tr.equals(op1))
    {
    ea=d*10;
    }
    else if(tr.equals(op2))
    {
    ea=d*5;
    }
    else if(tr.equals(op3))
    {
    ea=d*20;
    }
    else if(tr.equals(op4))
    {
    ea=d*10;
    }
    else if(tr.equals(op5))
    {
    ea=d*20;
    }
    else if(tr.equals(op5))
    {
    ea=d*15;
    }
    }
    else if(d<=500)
    {
    if(tr.equals(op1))
    {
    ea=d*10;
    }
    else if(tr.equals(op2))
    {
    ea=d*5;
    }
    else if(tr.equals(op3))
    {
    ea=d*20;
    }
    else if(tr.equals(op4))
    {
    ea=d*10;
    }
    else if(tr.equals(op5))
    {
    ea=d*20;
    }
    else if(tr.equals(op5))
    {
    ea=d*15;
    }
    }
    else if(d<=1000)
    {
    if(tr.equals(op1))
    {
    ea=d*10;
    }
    else if(tr.equals(op2))
    {
    ea=d*5;
    }
    else if(tr.equals(op3))
    {
    ea=d*20;
    }
    else if(tr.equals(op4))
    {
    ea=d*10;
    }
    else if(tr.equals(op5))
    {
    ea=d*20;
    }
    else if(tr.equals(op5))
    {
    ea=d*15;
    }
    }
    else if(d>1000)
    {
    if(tr.equals(op1))
    {
    ea=d*10;
    }
    else if(tr.equals(op2))
    {
    ea=d*5;
    }
    else if(tr.equals(op3))
    {
    ea=d*20;
    }
    else if(tr.equals(op4))
    {
    ea=d*10;
    }
    else if(tr.equals(op5))
    {
    ea=d*20;
    }
    else if(tr.equals(op5))
    {
    ea=d*15;
    }
    }
    }
    catch(Exception e)
    {

    }

    response.sendRedirect("service2.jsp");





    }





    }

    @@@@@@@@@@@@ this is the end of the servlet .Now the service.jsp is given below


    <%
    HttpSession sess=request.getSession();

    String dist=(String)sess.getAttribute("distance");
    String transport=(String)sess.getAttribute("service");
    String amt=(String)sess.getAttribute("estimateamt");
    %>
    <html>
    <body background="images\wall2.jpg">

    <b> Shifting distance:<%=dist%><br><br>
    <b>Shifting Type:<%=transport%><br><br>
    <b>Estimated Amount:<%=amt%><b><br><br>

    </td>
    <br>

    <br>

    <br><br>
    <br>
    </table>


    </table>

    </body>


    </html>

    Now when i run the servlet i am getting an error as follows :

    message

    descriptionThe server encountered an internal error () that prevented it from fulfilling this request.

    exception

    org.apache.jasper.JasperException: java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.String
    root cause

    java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.String

    I understand that the error has something to do with the estimate amount calculation and conversion of String to Long related..but i could not resolve the problem ..kindly help me where to make changes in the code or where i have done wrong..i am just a beginner...


  2. #2
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Getting Error in code pls help

    [sess.setAttribute("estimateamt",ea);

    -- U are assigning long to session

    String amt=(String)sess.getAttribute("estimateamt");

    --- U are retrieving Long from session but casting it to String, which is wrong so u r getting

    java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.String

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Getting Error in code pls help

    Quote Originally Posted by geminionweb View Post
    [sess.setAttribute("estimateamt",ea);

    -- U are assigning long to session

    String amt=(String)sess.getAttribute("estimateamt");

    --- U are retrieving Long from session but casting it to String, which is wrong so u r getting

    java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.String

    so what change should i make to correct the errors ??

Similar Threads

  1. Error in code
    By JCTexas in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 21st, 2011, 05:53 PM
  2. Simple code error
    By robin28 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 7th, 2010, 02:25 PM
  3. [SOLVED] Help with an error in code
    By cis170 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 6th, 2010, 09:21 AM
  4. Error with code
    By JJTierney in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 4th, 2010, 04:23 PM