-1

I have a simple Servlet.

public class MyServlet extends HttpServlet
{
...
}

Do I need to implement Serializable as well?

public class MyServlet extends HttpServlet implements Serializable
{
...
}

HttpServlet already implements Serializable. So, is it redundancy here?

What are the impacts if its implemented and if its not implemented?

user207421
  • 305,947
  • 44
  • 307
  • 483
user2488578
  • 896
  • 4
  • 21
  • 40
  • Same reason as to why you make your bean implement Serializable.. – user2488578 Oct 11 '13 at 04:42
  • 2
    A downvote is unnecessary since user2488578 clearly thought about it. He or she just doesn't have the experience to know why beans are different from servlets and how inheritance works. It isn't wrong to be a beginner. – Vidya Oct 11 '13 at 04:49
  • @Rupesh I wish to downvote your comments here. Or haven't you been a beginner? – Nikolas Charalambidis Sep 22 '16 at 16:53

5 Answers5

4

HttpServlet already implements Serializable, so even if you don't implement Serializable in your servlet, it still implements Serializable

More information on Serializable and why HttpServlet implements Serializable

What does Serializable mean?

Why does HttpServlet implement Serializable?

Community
  • 1
  • 1
Vivek
  • 1,640
  • 1
  • 17
  • 34
1

No, you don't need to do it. As you say, it is redundant because HttpServlet already implements Serializable.

Just don't forget to define the static final long serialVersionUID field in your MyServlet class.

Rahul
  • 44,383
  • 11
  • 84
  • 103
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
1

Whenever you extend a class, you extend everything about it. So since HttpServlet implements Serializable, Serializable comes along for the ride when you extend HttpServlet. No need to do it again yourself.

Vidya
  • 29,932
  • 7
  • 42
  • 70
0

See this : http://www.javapractices.com/topic/TopicAction.do?Id=45 I hope it's useful for you

Ba Tới Xì Cơ
  • 482
  • 4
  • 16
0

Serializable is a marker interface. it does not have any method of it's own. it is just a hint to jvm that the object of child classes would be serializable or writable to streams like file or network.

if a class already implement it, no need to do that again.

Pankaj Sharma
  • 1,833
  • 1
  • 17
  • 22